我正在尝试制作带有误差线的分组条形图。但是,我无法使误差条看起来正确(即比主条更薄)并且位置正确(位于条的中心)。这position
选项和position_dodge()
似乎工作不正常,我不明白为什么——根据其他类似问题的例子,这应该有效。
我正在 R 版本 3.4.2 中运行 ggplot2 版本 3.0.0。
一个最小的工作示例:
d<-data.frame(bin = factor(c(1,2,3,1,2,3)),type = factor(c(1,1,1,2,2,2)), beta = c(10,20,30,40,50,60), se = c(2,2,2,2,2,2))
ggplot(d, aes(x=bin,y=beta,ymin = beta - 1.96* se, ymax = beta+1.96* se)) +
geom_bar(aes(fill = type), position = position_dodge2(), stat="identity") +
geom_errorbar(position=position_dodge2(.9))
Produces:
Right position, but too wide. Using position_dodge()
instead of position_dodge2()
doesn't even get the position right:
ggplot(d, aes(x=bin,y=beta,ymin = beta - 1.96* se, ymax = beta+1.96* se)) +
geom_bar(aes(fill = type), position = position_dodge(), stat="identity") +
geom_errorbar(position=position_dodge(.9))
Adding the width
parameter to geom_errorbar
breaks the version with position_dodge2()
and doesn't help the one with position_dodge()
:
ggplot(d, aes(x=bin,y=beta,ymin = beta - 1.96* se, ymax = beta+1.96* se)) +
geom_bar(aes(fill = type), position = position_dodge2(), stat="identity") +
geom_errorbar(position=position_dodge2(.9), width= .2)
ggplot(d, aes(x=bin,y=beta,ymin = beta - 1.96* se, ymax = beta+1.96* se)) +
geom_bar(aes(fill = type), position = position_dodge(), stat="identity") +
geom_errorbar(position=position_dodge(.9), width = .2)
这里发生了什么?我该如何解决?