[R] adding layers in ggplot2 (data and code included)
hadley wickham
h.wickham at gmail.com
Mon Sep 22 16:03:06 CEST 2008
Hi Juliet,
On Sun, Sep 21, 2008 at 11:47 PM, Juliet Hannah <juliet.hannah at gmail.com> wrote:
> Here is some sample data:
>
> mydata <- read.table(textConnection("Est Group Tri
> 0 0 4.639644
> 1 0 4.579189
> 2 0 4.590714
> 0 1 4.443696
> 1 1 4.588243
> 2 1 4.650505
> 0 2 4.296608
> 1 2 4.826036
> 2 2 4.765386"),header=TRUE);
> closeAllConnections();
>
> I can form two plots, scatter and lines, as follows:
>
> p <- ggplot(mydata, aes(x=Est, y=Tri))
> p + geom_point(aes(colour=factor(Group),shape=factor(Group)))
>
> and
>
> p+ geom_smooth(aes(group=factor(Group),color=factor(Group)),method=lm,se=F).
>
> However, I am unable to have the plots together.
>
> I obtain the following error:
>
>> p + geom_point(aes(colour=factor(Group),shape=factor(Group)))+geom_smooth(aes(group=factor(Group),color=factor(Group)),method=lm,se=F)
> Error in `[.data.frame`(df, , var) : undefined columns selected
Are you using R 2.7.2? Something in R changed between R 2.7.1 and R
2.7.2 that breaks certain ggplot plots (you code works fine for me
without modification). It's on my to do list to fix.
You can also simplify your code a little by relying on defaults set in
the ggplot() call:
ggplot(mydata, aes(Est, Tri, colour = factor(Group))) +
geom_point(aes(shape = factor(Group))) +
geom_smooth(method = lm, se = F)
(Andpleaseusespacesotherwiseitsveryhardtoreadyourcode)
Hadley
>
> Thanks,
>
> Juliet
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
http://had.co.nz/
More information about the R-help
mailing list