ggplot2: how to group (x,y) and plot mean(y) per group?

186 views
Skip to first unread message

Samvel Khalatyan

unread,
Feb 22, 2014, 7:01:03 PM2/22/14
to ggp...@googlegroups.com
Hi,

I have an nlme::Oxboys dataset and plot height ~ Occasion. I can draw a smooth line through (x,y) points that are grouped by Subject:

require(ggplot2)
require(nlme)

ggplot(aes(x=Occasion, y=height), data=Oxboys) +
  geom_point() +
  geom_smooth(aes(group=Subject), se=F)

the output looks  like:

Is there a way to add mean per Occasion without modification of the data.frame ? The call below generates mean for the overall data.frame instead:

ggplot(aes(x=Occasion, y=height), data=Oxboys) +
  geom_point() +
  geom_smooth(aes(group=Subject), se=F) +
  geom_point(aes(group=Occasion, y=mean(height)), colour='red')

and the plots has next look (which is wrong):

Thanks.

Vivek Patil

unread,
Feb 22, 2014, 8:01:00 PM2/22/14
to Samvel Khalatyan, ggplot2
Does this do it?  (http://docs.ggplot2.org/0.9.3.1/stat_summary.html)

  ggplot(aes(x=Occasion, y=height), data=Oxboys) +
  geom_point() +geom_smooth(aes(group=Subject), se=F) +stat_summary(fun.y=mean, geom="point",color="red",size=3)



--
--
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
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Samvel Khalatyan

unread,
Feb 22, 2014, 8:18:06 PM2/22/14
to ggp...@googlegroups.com, Samvel Khalatyan
Yes, it does. Thanks.

But how can I draw a line through these points? When I change geom type from "point" to "line" in the geom_summary, the R gives an error message:

eom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

I guess, each point in the summary is treated independent.

Any ideas?

Vivek Patil

unread,
Feb 22, 2014, 8:43:48 PM2/22/14
to Samvel Khalatyan, ggplot2
This?
ggplot(aes(x=Occasion, y=height), data=Oxboys) +
  geom_point() +geom_smooth(aes(group=Subject), se=F) +stat_summary(fun.y=mean, geom="point",color="red",size=3)+
  stat_summary(fun.y=mean,geom="line",aes(group=1),size=1,color="black")

Dennis Murphy

unread,
Feb 22, 2014, 9:45:42 PM2/22/14
to Samvel Khalatyan, ggplot2
This example is on pp. 63-64 of the ggplot2 book. As an alternative to Vivek's solution:

require(ggplot2)
require(nlme)

ggplot(aes(x=Occasion, y=height), data=Oxboys) +
  geom_point() +
  geom_smooth(aes(group=Subject), se=F) +
  geom_smooth(aes(group = 1), color = "orange", size = 1, se = FALSE)

Dennis


On Sat, Feb 22, 2014 at 4:01 PM, Samvel Khalatyan <sn.kha...@gmail.com> wrote:

--

Samvel Khalatyan

unread,
Feb 23, 2014, 11:44:24 AM2/23/14
to ggp...@googlegroups.com, Samvel Khalatyan
Thanks, Vivek. This is exactly what I wanted. In fact, the original problem was to draw a line through quantiles. Now, it works fine:

ggplot(aes(x=Occasion, y=height), data=Oxboys) +
  geom_point() +
  geom_smooth(aes(group=Subject), se=F) +
  stat_summary(fun.y=function(y) { quantile(y)[4] },
               geom="point", color="red",size=3) +
  stat_summary(fun.y=function(y) { quantile(y)[4] },
               geom="line", aes(group=1),size=1,color="black")

Nevertheless I am a bit confused now. Would you please explain me how ggplot applies the fun.y? It seems that the function is applied per group=Occasion rather than the entire data set. However, we do not specify grouping parameter in the stat_summary. I am a bit confused at this point?

One more question, why do we need to specify group=1 in the line?

Thanks.
Reply all
Reply to author
Forward
0 new messages