7

I would like to produce a scatter plot with ggplot2, which contains both a regression line through all data points (regardless which group they are from), but at the same time varies the shape of the markers by the grouping variable. The code below produces the group markers, but comes up with TWO regression lines, one for each group.

#model=lm(df, ParamY~ParamX)
p1<-ggplot(df,aes(x=ParamX,y=ParamY,shape=group)) + geom_point() + stat_smooth(method=lm)

How can I program that?

Doc
  • 358
  • 1
  • 4
  • 24

1 Answers1

12

you shouldn't have to redo your full aes in the geom_point and add another layer, just move the shape aes to the geom_point call:

df <- data.frame(x=1:10,y=1:100+5,grouping = c(rep("a",10),rep("b",10)))
ggplot(df,aes(x=x,y=y)) + 
geom_point(aes(shape=grouping)) + 
stat_smooth(method=lm)

enter image description here

EDIT:

To help with your comment:

because annotate can end up, for me anyway, with the same labels on each facet. I like to make a mini data.frame that has my variable for faceting and the facet levels with another column representing the labels I want to use. In this case the label data frame is called dfalbs.

Then use this to label data frame to label the facets individually e.g.

df <- data.frame(x=1:10,y=1:10,grouping =   
                  c(rep("a",5),rep("b",5)),faceting=c(rep(c("oneR2","twoR2"),5)))


dflabs <- data.frame(faceting=c("oneR2","twoR2"),posx=c(7.5,7.5),posy=c(2.5,2.5))

ggplot(df,aes(x=x,y=y,group=faceting)) + 
       geom_point(aes(shape=grouping),size=5) + 
       stat_smooth(method=lm) +
       facet_wrap( ~ faceting) +
       geom_text(data=dflabs,aes(x=posx,y=posy,label=faceting))

enter image description here

user1317221_G
  • 15,087
  • 3
  • 52
  • 78
  • 1
    I agree, that is definitely some elegant code. When I resort to facet, could I separately annotate the lines (for each group/facet)? – Doc Jan 14 '13 at 12:45
  • by annotate do you mean text or just draw the lines? – user1317221_G Jan 14 '13 at 12:55
  • I meant `+ annotate("text", label=)`. If I have two regression lines in the facets, I´d like to write the different R2 and p-values in the graphs. – Doc Jan 15 '13 at 15:40
  • in my experience `annotate` ends up putting the same labels on each facet. I like to make a small `data.frame` with labels and facets and then use `geom_text` with the new small `data.frame` – user1317221_G Jan 15 '13 at 15:42