在多面条形图上重新定位 geom_errorbar

Reposition geom_errorbar on a faceted bargraph

我在 ggplot2 中发现了很多关于重新定位错误栏的问题,但是 none 有我的特殊问题。我希望这不是重复的!

我有一个多面条形图,我试图在其中为已经计算出的置信区间添加误差条。参数 statposition 似乎没有任何效果,无论它们是在 aes() 参数中还是仅与 geom_errorbar() 一起使用。这是我正在使用的:

> dput(anthro.bp)
structure(list(Source = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("PED", "RES"), class = "factor"), 
Response = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"
), class = "factor"), Value = c(0.5043315, 0.03813694, 0.20757498, 
0.249956615, 0.9232598, 0.0142572, 0.0537258, 0.008757155, 
0.897265, 0.03153401, 0.06610772, 0.005093254, 0.8360081, 
0.03893782, 0.0370325, 0.088021559), Distance = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Near", "Far"), class = "factor"), UCI = c(0.5853133, 
0.07247573, 0.27357566, 0.32335335, 0.9744858, 0.03844421, 
0.08841988, 0.04262752, 0.9422062, 0.0540748, 0.09600908, 
0.03348959, 1.2445932, 0.11196198, 0.10133358, 0.52272511
), LCI = c(0.4233497, 0.003798153, 0.1415743, 0.17655988, 
0.8720338, -0.009929805, 0.01903172, -0.02511321, 0.8523238, 
0.008993231, 0.03620636, -0.02330308, 0.427423, -0.034086335, 
-0.02726858, -0.34668199)), .Names = c("Source", "Response", 
"Value", "Distance", "UCI", "LCI"), row.names = c(NA, -16L), class = "data.frame")

anthro.bp[,4]<-factor(anthro.bp[,4], levels=c("Near","Far"))
bp <- ggplot(anthro.bp, aes(Value, fill=Response))
bp + geom_bar(aes(x=Source,y=Value), stat="identity", position="dodge") +
   geom_errorbar(aes(ymin=LCI,ymax=UCI), stat="identity", position="dodge",width=0.25) +
   facet_wrap(~Distance) +
   labs(x="Disturbance Source", y="Mean Probability") 

我也曾尝试在 aes() 参数内和 geom_errorbar() 命令外使用 position=position_dodge(width=1)。我的图表如下 link(我还没有足够高的声誉来嵌入图像,抱歉!)。

我还收到两条错误消息:

Warning messages:
1: In loop_apply(n, do.ply) :
position_dodge requires non-overlapping x intervals
2: In loop_apply(n, do.ply) :
position_dodge requires non-overlapping x intervals

这是我第一次在课堂环境之外使用 ggplot2,因此非常鼓励建设性的批评。

出于某种我不清楚的原因,ggplot2 正在用不同的值避开你的条和误差条。我通过手动指定闪避宽度解决了这个问题。此外,您仅设置了 y 和 x 美学 geom_bar。请注意它们现在放置的位置。最后,geom_errorbar 调用不需要 stat='identity'。

bp <- ggplot(anthro.bp, aes(x=Source,y=Value, fill=Response))
bp + geom_bar(stat="identity", position = position_dodge(width = 0.90)) +
  geom_errorbar(aes(ymin=LCI,ymax=UCI), position = position_dodge(width = 0.90),width=0.25) +
  facet_wrap(~Distance) +
  labs(x="Disturbance Source", y="Mean Probability") 

根据How to make dodge in geom_bar agree with dodge in geom_errorbar, geom_point

是你想要的吗?

anthro.bp$dmin <-  anthro.bp$Value - anthro.bp$LCI
anthro.bp$dmax <- anthro.bp$UCI - anthro.bp$Value

ggplot(data=anthro.bp, aes(x=Source, ymin=Value-dmin, ymax=Value+dmax, fill=Response)) +
geom_bar(position=position_dodge(), aes(y=Value), stat="identity") +
geom_errorbar(position=position_dodge(width=0.9), colour="black") + facet_wrap(~Distance) + labs(x="Disturbance Source", y="Mean Probability")

我认为您的主要问题是 bp 的 aes 中未定义的 x 值(来源)。

bp <- ggplot(anthro.bp, aes(x=Source,y=Value, fill=Response))
bp + geom_bar(stat="identity", position=position_dodge()) +
geom_errorbar(aes(ymin=LCI,ymax=UCI),width=0.25, stat="identity", position=position_dodge(0.9)) +
facet_wrap(~Distance) +
labs(x="Disturbance Source", y="Mean Probability")