[英]Error bars not plotting correctly on barplot
I am having trouble getting my error bars to plot correctly. 我无法正确显示错误条。 Here is my data frame:
这是我的数据框:
> 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
I am using this code to produce a grouped barplot: 我正在使用此代码来生成分组的barplot:
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))
For some reason the error bars are not plotting correctly and the graph looks like this: 由于某些原因,误差线无法正确绘制,并且图形如下所示:
Hopefully the solution is a simple one, however it is eluding me. 希望解决方案是一个简单的解决方案,但是它使我难以理解。 Thanks in advance for any help!
在此先感谢您的帮助!
Updated in light of your comment - for this type of plot, you can assign the value for position_dodge
to a variable and use that for all subsequent plots. 根据您的评论进行了更新-对于这种类型的绘图,您可以将
position_dodge
的值分配给变量,并将其用于所有后续绘图。 I hit on the value of 20
with a little trial and error; 我经过几次尝试和错误尝试将值设为
20
; if the other data you're working with is very different numerically, you may need to fine tune this. 如果您使用的其他数据在数值上有很大差异,则可能需要对此进行微调。
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))
giving: 赠送:
I think the problem you were having is that using position="dodge"
allows ggplot2
to calculate an 'optimal' dodge. 我认为您遇到的问题是使用
position="dodge"
允许ggplot2
计算“最佳”躲闪。 However you need to see/state the value explicitly to pass it as an argument to geom_errorbar
. 但是,您需要显式查看/声明该值,以将其作为参数传递给
geom_errorbar
。
Setting the width
in geom_bar
to the same value as position_dodge
in both geom_bar
and geom_errorbar
(here pd1 <- 20
) overcomes this. 将
geom_bar
的width
设置为与geom_bar
和geom_errorbar
position_dodge
相同的值(此处pd1 <- 20
)可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.