r - 如何使用 ggplot2 将标准误差线添加到箱须图?

标签 r graph ggplot2

我正在尝试将标准误差线添加到我的数据中,类似于此问题答案末尾附近的箱形图上看到的误差线: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

我制作了这个情节

this plot

使用以下代码

 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/

相关文章:

R:堆叠面积图不堆叠

python - 寻找库/工具来可视化多维数据

5亿(双)值的Java数据结构?

r - 防止 ggplot2 图例重新排序标签

r - 如何将碎石图比例设置为与主成分相同?

r - 在 R 基础图形或 ggplot 中使用动画散点图可能会发光吗?

r - 在 R 中查找长度大于 1 的向量元素

r - 抓取请求 rvest 同意 cookie 的站点

r - 将已排序数据框中的最近值绘制到未排序数据框中

algorithm - 通过传送找到图中的最短路径