我正在尝试将标准误差线添加到我的数据中,类似于此问题答案末尾附近的箱形图上看到的误差线: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:二元运算符的非数字参数”
如有任何帮助,我们将不胜感激。谢谢
最佳答案
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")
关于r - 如何使用 ggplot2 将标准误差线添加到箱须图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599335/