我发现很多问题涉及在ggplot2
中重新定位错误条,但没有一个问题存在我的问题。我希望这不是重复的!
我有一个刻面的条形图,我试图在已经计算的置信区间中添加误差条。参数stat
和position
似乎没有任何影响,无论它们是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")
我还试图在position=position_dodge(width=1)
参数内和aes()
命令之外再次使用geom_errorbar()
。我的图表在链接中如下(我没有足够的声誉来嵌入图片,道歉!)。
我还收到两条错误消息:
警告讯息:
1:在loop_apply(n,do.ply)中:
position_dodge需要不重叠的x间隔
2:在loop_apply(n,do.ply)中:
position_dodge需要不重叠的x区间
这是我第一次在课堂环境之外使用ggplot2,因此非常鼓励建设性的批评。
答案 0 :(得分:2)
由于某些我不清楚的原因,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")
答案 1 :(得分:0)
根据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")
答案 2 :(得分:0)
我认为你的主要问题是bp的aes中未定义的x值(Source)。
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")