我试图在我的数据中添加标准错误栏,类似于在这个问题的答案末尾附近的方框图上看到的那些:https://stats.stackexchange.com/questions/8137/how-to-add-horizontal-lines-to-ggplot2-boxplot
我正在使用PlantGrowth数据集,它看起来像这样(除了总共30行之外):
weight group
1 4.17 ctrl
2 5.58 ctrl
3 4.81 trt1
4 4.17 trt1
5 6.31 trt2
6 5.12 trt2
我制作了这个情节
使用以下代码
ggplot(PlantGrowth, aes(group, weight))+
stat_boxplot(geom='errorbar', linetype=1, width=0.5)+ #whiskers
geom_boxplot(outlier.shape=1)+
stat_summary(fun.y=mean, geom="point", size=2) #dot for the mean
我不知道如何根据该因子变量中的变化为每个绘图添加误差条。我添加了+geom_errorbar(aes(x=group, ymin=mean-sd, ymax=mean+sd))
但是它返回错误"错误的意思是 - sd:非数字参数到二元运算符"
任何帮助将不胜感激。谢谢
答案 0 :(得分:7)
ggplot2中有一个mean_se
函数,可以完全按照您的要求执行。
library(ggplot2)
ggplot(PlantGrowth, aes(group, weight))+
stat_boxplot( aes(group, weight),
geom='errorbar', linetype=1, width=0.5)+ #whiskers
geom_boxplot( aes(group, weight),outlier.shape=1) +
stat_summary(fun.y=mean, geom="point", size=2) +
stat_summary(fun.data = mean_se, geom = "errorbar")