【问题标题】:Error bars not plotting correctly on barplot误差线在 barplot 上未正确绘制
【发布时间】:2015-05-02 14:23:19
【问题描述】:

我无法正确绘制误差线。这是我的数据框:

> d
         Date Plate.1 Plate.2 Plate.3 Plate.4 Plate.5 Plate.6 Plate.7 Plate.8 Sum Average        SE Treatment
1  2014-10-15       2       2       0       5       8      11      11       0  39   4.875 1.6304852         1
2  2014-11-12       5       4      11       6       7       2       9       6  50   6.250 0.9955257         1
3  2014-12-11       1       0       0       0       0       0       0       1   2   0.250 0.1636634         1
4  2015-02-11       0       0       4       0       2       1       0       1   8   1.000 0.5000000         1
5  2015-03-09       0       0       0       0       0       0       0       0   0   0.000 0.0000000         1
6  2014-10-15      30      22      24      19      10      16      19      42 182  22.750 3.4369318         2
7  2014-11-12       8       5       4       9      12      23      14      10  85  10.625 2.1207942         2
8  2014-12-11       0       5      21      19       2       9       4       0  60   7.500 2.9215945         2
9  2015-02-11      16      11       5       8       5      17       0       0  62   7.750 2.3126207         2
10 2015-03-09       0       0       0       1       0       0       0       0   1   0.125 0.1250000         2

我正在使用此代码生成分组条形图:

ggplot(d, aes(x=Date, y=Average, fill = Treatment)) +
  geom_bar(position="dodge", stat="identity") +
  geom_errorbar(aes(ymin=Average-SE, ymax=Average+SE),
                width=.2,                    
                position = position_dodge(.9))

由于某种原因,误差线没有正确绘制,图表如下所示:

希望解决方案是一个简单的解决方案,但它让我望而却步。提前感谢您的帮助!

【问题讨论】:

    标签: r ggplot2 bar-chart standard-error


    【解决方案1】:

    根据您的评论进行了更新 - 对于这种类型的绘图,您可以将 position_dodge 的值分配给一个变量并将其用于所有后续绘图。 我通过一些试验和错误找到了20 的值;如果您正在处理的其他数据在数值上非常不同,您可能需要对此进行微调。

    d <- data.frame(Date=c("2014-10-15", "2014-11-12", "2014-12-11",
                    "2015-02-11", "2015-03-09", "2014-10-15",
                    "2014-11-12", "2014-12-11", "2015-02-11",
                    "2015-03-09"),
                    Average=c(4.875, 6.250, 0.25, 1, 0,
                    22.75, 10.625, 7.5, 7.75, 0.125),
                    Treatment=c(rep(1, 5), rep(2, 5)),
                    SE=c(1.6304852, 0.9955257, 0.1636634, 0.5, 0,
                    3.4369318, 2.1207942, 2.9215945, 2.3126207, 0.125))
    d$Date <- as.Date(d$Date)
    d$Treatment <- factor(d$Treatment)
    library(ggplot2)
    pd1 <- 20
    ggplot(d, aes(x=Date, y=Average, fill=Treatment)) + 
        geom_bar(width=pd1,
                 position=position_dodge(pd1),
                 stat="identity") +
        geom_errorbar(aes(ymin=Average-SE, ymax=Average+SE),
                      width=pd1 / 2,                    
                      position=position_dodge(pd1))
    

    给予:

    我认为您遇到的问题是使用position="dodge" 允许ggplot2 计算“最佳”闪避。但是,您需要显式查看/声明该值以将其作为参数传递给 geom_errorbar

    geom_bar 中的width 设置为与geom_bargeom_errorbar(此处为pd1 &lt;- 20)中的position_dodge 相同的值可以解决此问题。

    【讨论】:

      猜你喜欢
      • 2013-03-15
      • 1970-01-01
      • 2015-08-12
      • 2020-08-09
      • 2022-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多