使用stat_summary的标准错误栏 [英] Standard error bars using stat_summary

查看:189
本文介绍了使用stat_summary的标准错误栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码使用Hmisc,ddply和ggplot生成带有标准错误栏的柱状图:

  means_se < -  ddply mtcars,。(cyl),
函数(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()

然而,执行上述使用诸如mean_sdl之类的帮助函数似乎好得多。例如,以下代码会生成一个包含95%CI误差线的图:

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

我的问题是如何将stat_summary实现用于标准错误栏。问题是,要计算SE,您需要每个条件的观察次数,并且必须通过mean_sdl的乘数访问。



如何在ggplot中访问此信息?有没有一个整洁的非hacky的解决方案呢?

解决方案

好吧,我无法告诉你如何获得乘数分成 stat_summary 。然而,看起来你的目标是绘制均值和误差线,它们代表 ggplot Hmisc 中的 mean_cl_normal 函数并将乘数更改为1,

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

编辑更新ggplot_2.0.0



ggplot2 version 2.0.0,需要传递给您正在使用的汇总函数的参数需要作为 fun.args 参数的列表给出。 mult 参数是 mean_cl_normal 的参数。

<$ p $ func = func = gent =bar)+
stat_summary(fun.data =+); $ p $ ggplot(mtcars,aes(cyl,qsec))+
stat_summary然而,在这种情况下,我们可能会遇到一些问题,例如,现在我们可以使用 ggplot2 中的 mean_se 函数来替代 mean_cl_normal from Hmisc mean_se 函数的默认乘数为1,所以如果我们需要标准误差条,我们不需要传递任何额外的参数。

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


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?

解决方案

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. Here is an easy way to do that, using the mean_cl_normal function from Hmisc and changing the multiplier to 1 so you get one standard error from the mean.

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

EDIT Update for ggplot_2.0.0

Starting in ggplot2 version 2.0.0, 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. The mult argument is an argument for mean_cl_normal.

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

However, there is now 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")

这篇关于使用stat_summary的标准错误栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆