geom_smooth for all points

759 views
Skip to first unread message

Agustin Lobo

unread,
May 30, 2012, 9:05:00 AM5/30/12
to ggp...@googlegroups.com
Hi!

I'm doing this

qplot(doy2,NEE_st,data=delme,geom="point",size=I(0.75),facets=YEAR~.,colour=as.factor(cod))
+ geom_smooth() + scale_colour_manual(values=c("green", "red"))

but get 2 smoothed lines, and I would like one single line (in some
cases fitting all points, in other cases fitting either only the green
or the red, but not 2 lines)

Data here:
http://dl.dropbox.com/u/3180464/delme.rda

Thanks

Agus

Brandon Hurr

unread,
May 30, 2012, 9:25:58 AM5/30/12
to Agusti...@ictja.csic.es, ggp...@googlegroups.com
I would separate it out so geom_smooth() gets the whole dataset. 

ggplot(data=delme, aes(x=doy2, y=NEE_st))+
geom_point(aes(colour=as.factor(cod)), size=0.75)+
geom_smooth()+
facet_wrap(YEAR~.)+
scale_colour_manual(values=c("green", "red"))

May not be perfect, but it should work. 

HTH, 

B



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Agustin Lobo

unread,
May 30, 2012, 1:41:30 PM5/30/12
to Brandon Hurr, ggp...@googlegroups.com
Brandon,
I get an error with facet_wrap():
> ggplot(data=delme, aes(x=doy2, y=NEE_st))+
+ geom_point(aes(colour=as.factor(cod)), size=0.75)+
+ geom_smooth()+
+ facet_wrap(YEAR~.)+
+ scale_colour_manual(values=colorin)
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting

Thanks,
Agus

2012/5/30 Brandon Hurr <brando...@gmail.com>:

Brandon Hurr

unread,
May 30, 2012, 1:51:35 PM5/30/12
to Agusti...@ictja.csic.es, ggp...@googlegroups.com
Maybe remove the "."?  I'm away from my computer so I can't really play much. 

Dennis Murphy

unread,
May 30, 2012, 6:48:53 PM5/30/12
to Agusti...@ictja.csic.es, ggp...@googlegroups.com
Hi:

Does this work for you?

ggplot(delme, aes(x = doy2, y = NEE_st)) +
geom_point(aes(colour = as.factor(cod))) +
geom_smooth(size = 1, colour = 'blue') +
labs(x = 'Day of year', colour = 'cod') +
facet_grid(YEAR ~ .) +
scale_colour_manual(values = c('orange', 'purple'))

I changed the point sizes to reduce the effect of overplotting and the
colors to be friendlier to those with color blindness. To get colors
by cod level but a single overall smooth for a given year, you need to
remove colour as an aesthetic in the top level qplot/ggplot() call and
reserve it for the geom_point() layer, as Brandon showed you. When
colour = is in the top level call, it is assumed to be present in
*all* subsequent layers. In this case, you want color differentiation
in the geom_point() layer but not the geom_smooth() layer, which is
why you don't want colour = in the qplot/ggplot() call.

HTH,
Dennis
Reply all
Reply to author
Forward
0 new messages