[R] Question concerning ggplot

hadley wickham h.wickham at gmail.com
Tue Oct 24 14:52:56 CEST 2006


> > 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:

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



More information about the R-help mailing list