简体   繁体   English

如何使用 R 中的 ggplot2 绘制均值的误差线?

[英]How to plot error bars for a mean with ggplot2 in R?

I have plotted my data as a scatter bar plot and have a benchmark (Basemodel) and a theoretical value (AASHTO) that I am presenting with different colored points.我已经将我的数据绘制为散点图,并有一个基准(基本模型)和一个理论值(AASHTO),我用不同的颜色点来表示。 I use the following code and my graph looks as follows:我使用以下代码,我的图表如下所示:

library(ggplot2)
ggplot(data = plotshear, aes(x = Shear, y = LLDF)) + geom_jitter(aes(colour = Source))

# plotshear data:

      Source     Shear        LLDF
# 1   Baseline   EXT.Single   0.7627894
# 2   Samples    EXT.Single   0.7130376
# 3   Baseline   EXT.Multi    0.8521338
# 4   Samples    EXT.Multi    0.7975502
# 5   Baseline   INT.Single   0.5706947
# 6   Samples    INT.Single   0.5462812
# 7   Baseline   INT.Multi    0.7291602
# 8   Samples    INT.Multi    0.7331171
# 9   AASHTO     EXT.Multi    0.8250000
# 10  AASHTO     INT.Single   0.7200000
# 11  AASHTO     INT.Multi    0.8840000

I actually need a error bar for a mean looking like this:我实际上需要一个误差线来表示看起来像这样的平均值:

current plot当前情节

当前情节

plot needed需要情节

需要情节

Here is a solution that reproduces the "plot needed" figure from your data:这是一个解决方案,可从您的数据中重现“所需的绘图”图:

library(ggplot2)

# plotshear data:
plotshear <- data.frame(Source = c(rep(c("Baseline", "Samples"), 4), rep("AASHTO", 3)),
                        Shear = c(rep("EXT.Single", 2), rep("EXT.Multi", 2), rep("INT.Single", 2),
                                  rep("INT.Multi", 3), "INT.Single", "INT.Multi"),
                        LLDF = c(0.7627894, 0.7130376, 0.8521338, 0.7975502, 0.5706947,
                                 0.5462812, 0.7291602, 0.7331171, 0.8250000, 0.7200000, 0.8840000))

#without error bars
ggplot(data = plotshear, aes(x = Shear, y = LLDF)) +
  geom_boxplot() +
  geom_jitter(aes(color = Source)) +
  stat_summary(fun.y=mean, geom="point", shape=23, size=4) +
  stat_summary(fun.y=mean, geom="line", aes(group=1)) 

#with error bars
ggplot(data = plotshear, aes(x = Shear, y = LLDF)) +
  geom_boxplot() +
  geom_jitter(aes(color = Source)) +
  stat_summary(fun.y=mean, geom="point", shape=23, size=4) +
  stat_summary(fun.y=mean, geom="line", aes(group=1)) +
  stat_summary(fun.data = mean_se, geom = "errorbar")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM