[R] Question concerning ggplot

Rainer M Krug RKrug at sun.ac.za
Tue Oct 24 15:24:44 CEST 2006


hadley wickham wrote:
>> > Do you mean doing a seperate linear regression for each group in your
>> > data?  If so, have a look at ?gggroup for some examples of exactly
>> > that.
>>
>> No.
>>
>> I do the following:
>>
>>  > p <- ggplot(cc, . ~ expert, aesthetic=list(y=x0050, x=generation))
>>  > ggjitter(p)
>>  > ggsmooth(method=lm)
>>
>> And I get six columns in the graph, one for each expert and
>> ggsmooth(method=lm) draws the linear regression lines into the graphs.
>> I would like to have the coefficients and the R-square values of the
>> linear regressions to analyse them further and to print them in the 
>> graphs.
> 
> Ok - In that case you have to do the regressions yourself and add the
> lines etc.  Maybe something along these lines:

Thanks a lot - I thought so. I'll take a look at your suggestions below 
and come back to you if I don't get it to work.

Rainer


> 
> p <- ggpoint(ggplot(mtcars, aes=list(x=wt, y=mpg), formula= . ~ cyl))
> 
> models <- stamp(mtcars, . ~ . | cyl, function(df) lm(mpg ~ wt, data=df))
> lapply(models, coef)
> sapply(models, function(x) summary(x)$r.squared)
> 
> or
> rsqs <- as.data.frame(stamp(mtcars, cyl ~ ., function(df)
> summary(lm(mpg ~ wt, data=df))$r.squared))
> rsqs$wt <- 4
> rsqs$mpg <- 32
> ggtext(p, data=rsqs, aes=list(label=value))
> 
> But that doesn't work for some reason I don't understand yet (but I'm
> working on it)
> 
> Hadley


-- 
Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation
Biology (UCT)

Department of Conservation Ecology and Entomology
University of Stellenbosch
Matieland 7602
South Africa

Tel:		+27 - (0)72 808 2975 (w)
Fax:		+27 - (0)21 808 3304
Cell:		+27 - (0)83 9479 042

email:	RKrug at sun.ac.za
       	Rainer at krugs.de



More information about the R-help mailing list