Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

r - Standard error bars using stat_summary

The following code produces bar plots with standard error bars using Hmisc, ddply and ggplot:

means_se <- ddply(mtcars,.(cyl),
                  function(df) smean.sdl(df$qsec,mult=sqrt(length(df$qsec))^-1))
colnames(means_se) <- c("cyl","mean","lower","upper")
ggplot(means_se,aes(cyl,mean,ymax=upper,ymin=lower,group=1)) + 
  geom_bar(stat="identity") +  
  geom_errorbar()

However, implementing the above using helper functions such as mean_sdl seems much better. For example the following code produces a plot with 95% CI error bars:

ggplot(mtcars, aes(cyl, qsec)) + 
  stat_summary(fun.y = mean, geom = "bar") + 
  stat_summary(fun.data = mean_sdl, geom = "errorbar")

My question is how to use the stat_summary implementation for standard error bars. The problem is that to calculate SE you need the number of observations per condition and this must be accessed in mean_sdl's multiplier.

How do I access this information within ggplot? Is there a neat non-hacky solution for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, I can't tell you how to get a multiplier by group into stat_summary.

However, it looks like your goal is to plot means and error bars that represent one standard error from the mean in ggplot without summarizing the dataset before plotting.

There is a mean_se function in ggplot2 that we can use instead of mean_cl_normal from Hmisc. The mean_se function has a multiplier of 1 as the default so we don't need to pass any extra arguments if we want standard error bars.

ggplot(mtcars, aes(cyl, qsec)) + 
    stat_summary(fun.y = mean, geom = "bar") + 
    stat_summary(fun.data = mean_se, geom = "errorbar")

If you want to use the mean_cl_normal function from Hmisc, you have to change the multiplier to 1 so you get one standard error from the mean. The mult argument is an argument for mean_cl_normal. Arguments that you need to pass to the summary function you are using needs to be given as a list to the fun.args argument:

ggplot(mtcars, aes(cyl, qsec)) + 
    stat_summary(fun.y = mean, geom = "bar") + 
    stat_summary(fun.data = mean_cl_normal, geom = "errorbar", fun.args = list(mult = 1))

In pre-2.0 versions of ggplot2, the argument could be passed directly:

ggplot(mtcars, aes(cyl, qsec)) + 
  stat_summary(fun.y = mean, geom = "bar") + 
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", mult = 1) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...