ggplot2: stat_summary

47 views
Skip to first unread message

Sara Fung

unread,
Dec 14, 2012, 10:05:45 PM12/14/12
to hk...@googlegroups.com
Hi there, I don't understand what's wrong with the stat_summary(...aes(group=1)) or stat_summary(...aes(group=IV)), since it supposed to be able to make a line connecting 4 means, why it doesn't and returned with a nonsense question?  I assume it is something wrong with the command but I can't figure it out, please help. Thank you so much. s


linegraph<-ggplot(dataframe,aes(IV,DV))#IV has 4 levels

linegraph+stat_summary(fun.y=mean,geom="point")#it returned 4 points in a graph

linegraph+stat_summary(fun.y=mean,geom="point")+stat_summary(fun.y=mean, geom="line",aes(group=1))#wanna make a line to connect 4 points but it returns: 


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



Chung-hong Chan

unread,
Dec 15, 2012, 1:59:42 AM12/15/12
to hk...@googlegroups.com
With no example data provided for tinkering, it is hard for people to understand your problem.
But I have the super power to sense your problem. I think your data should be similar to the example iris dataset.

I found the stat_summary function insanely difficult to work with. My approach (not necessarily a good approach) is first summarize the data using the summary function (in your case, mean) before plotting using ggplot.

require(doBy)
summary.iris<-summaryBy(Sepal.Length ~ Species, data = iris, FUN = mean, keep.names = TRUE)
ggplot(summary.iris, aes(x=Species, y=Sepal.Length)) + geom_point()+geom_line(aes(x=as.numeric(Species), y=Sepal.Length))

The problem should be the x argument in geom_line should be numeric rather than a factor. However, I don't know how to pass it via the stat_summary function.

Sara Fung

unread,
Dec 17, 2012, 4:00:59 AM12/17/12
to hk...@googlegroups.com
Hey Hong,

Thank you for your method, it's work.
Before I give up, I googled it and found it is an unsolved problem.  People with problem like mine, turn to use geom_line() instead of stat_summary. s
Reply all
Reply to author
Forward
0 new messages