With stat="summary", fun.y="mean" get "Each group consist of only one observation"

6,072 views
Skip to first unread message

Etienne Low-Decarie

unread,
Nov 28, 2012, 4:29:48 PM11/28/12
to ggp...@googlegroups.com
I have a script that has the form

d <- qplot(cyl, mpg, data=mtcars)
d + geom_line(stat="summary", fun.y="mean")

Which now produces

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


I am running ggplot2 0.9.2.99 on R  2.15.2

This used to work.  Any suggestions?

Thank you so much for the incredible package!

Etienne Low-Decarie

unread,
Nov 28, 2012, 4:38:56 PM11/28/12
to ggp...@googlegroups.com
To be more precise, my code is more of the form
d <- qplot(qsec, mpg, data=mtcars, colour=as.character(cyl), shape=as.character(gear))
d + geom_line(stat="summary", fun.y="mean", aes(group=as.character(cyl)))

So just using:
stat_sum_single <- function(fun, geom="point", ...) { stat_summary(fun.y=fun, colour="red", geom=geom, size = 3, ...) }
Does not seem to work.

Etienne Low-Decarie

unread,
Nov 28, 2012, 4:41:48 PM11/28/12
to ggp...@googlegroups.com
stat_sum_single <- function(fun, geom="point", ...) {
  stat_summary(fun.y=fun, colour="red", geom=geom, size = 3, ...)
}

d + stat_sum_single(mean, geom="line", aes(group=as.character(cyl)))

Also gives 

Dennis Murphy

unread,
Nov 28, 2012, 5:40:25 PM11/28/12
to Etienne Low-Decarie, ggp...@googlegroups.com
d <- qplot(qsec, mpg, data=mtcars, colour=as.character(cyl),
shape=as.character(gear))
d + stat_summary(fun.y = mean, geom = "line",
aes(group = as.character(cyl))) +
labs(colour = "cyl", shape = "gear")

Is that what you were after?
Dennis

Etienne Low-Decarie

unread,
Dec 5, 2012, 11:48:36 AM12/5/12
to ggp...@googlegroups.com, Etienne Low-Decarie
This also produces geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?  with no path being plotted.

Thank you


library(ggplot2)
> d <- qplot(qsec, mpg, data=mtcars, colour=as.character(cyl), 
+            shape=as.character(gear)) 
> d + stat_summary(fun.y = mean, geom = "line", 
+                  aes(group = as.character(cyl))) + 
+                      labs(colour = "cyl", shape = "gear") 
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

Winston Chang

unread,
Dec 5, 2012, 12:32:55 PM12/5/12
to Etienne Low-Decarie, ggplot2
I think the following _should_ work, but doesn't:
ggplot(mtcars, aes(x=cyl, y=mpg)) +
  geom_point() +
  stat_summary(fun.y=mean, geom="line", aes(group=factor(cyl)))


This may be a bug in the new version. Could you file an issue on github?

-Winston


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

Morten Lindow

unread,
Jan 10, 2013, 3:05:03 AM1/10/13
to ggp...@googlegroups.com
I would be grateful if any would share a solution to this problem.  Thanks!

Morten

Dennis Murphy

unread,
Jan 10, 2013, 3:30:06 AM1/10/13
to Morten Lindow, ggp...@googlegroups.com
Since the stat_summary() examples in the current documentation
indicate the same error message, the safe thing to do in the interim
is probably to do the summarization outside of ggplot2 in a separate
data frame and then use it in the layers where you need it.

Here is an example for a simple interaction plot:

library(ggplot2)
library(plyr)

# Produce a data frame of mean(mpg) for vs/am combinations
mtm <- ddply(mtcars, .(vs, am), summarise, mn = mean(mpg))

# interaction plot
ggplot(mtm, aes(x = factor(vs), y = mn, group = factor(am))) +
geom_point(size = 3) + geom_line()


# The error message appears if I try something like this in 0.9.3:

ggplot(mtcars, aes(x = factor(vs), y = mpg) +
stat_summary(fun.y = mean, aes(group = factor(am)),
geom = "point", size = 3) +
stat_summary(fun.y = mean, aes(group = factor(am)),
geom = "line", size = 1)

The first stat_summary() call works, but not the second.

I looked around yesterday for open or closed issues on GitHub
regarding this topic, but didn't find one, so I don't know offhand if
this has been fixed in the development version or not.

Dennis

On Thu, Jan 10, 2013 at 12:05 AM, Morten Lindow <morten...@gmail.com> wrote:
> I would be grateful if any would share a solution to this problem. Thanks!
>
> Morten
>

Bryan Hanson

unread,
Jan 10, 2013, 7:35:13 AM1/10/13
to Dennis Murphy, Morten Lindow, ggp...@googlegroups.com
It's issue 739.

Also, it seems all issues have been closed. Am I looking in the right place?

https://github.com/hadley/ggplot2/issues?milestone=1&page=2&state=closed

Thanks, Bryan

(and thanks Dennis for a suggested fix)

Brandon Hurr

unread,
Jan 10, 2013, 8:34:58 AM1/10/13
to Bryan Hanson, Dennis Murphy, Morten Lindow, ggplot2

Bryan Hanson

unread,
Jan 10, 2013, 9:50:30 AM1/10/13
to Brandon Hurr, Dennis Murphy, Morten Lindow, ggplot2
Thanks all.  I knew it didn't look right!
Reply all
Reply to author
Forward
0 new messages