我的条形图有问题 - 误差条只出现在分组变量列的角落,而不是集中显示在它们上。我正在使用的代码是这样的:
a <- data.frame (Cond = c("In", "In", "Out", "Out"),
Temp = c("Hot", "Cool", "Hot", "Cool"),
Score = c(.03, -.15, 0.84, 0.25),
SE = c(.02, .08, .14, .12))
a.bar <- ggplot (data = a, aes(x = Cond, y = Score, fill = Temp)) +
theme_bw() + theme(panel.grid = element_blank ()) +
coord_cartesian (ylim = c(-0.5, 1)) +
geom_bar (aes(fill = Temp), stat = "identity", position = "dodge", width = .5) +
geom_errorbar (aes(ymin = Score - SE, ymax = Score + SE, group = Cond), position = position_dodge(.9), width = .08) +
labs(y = "Scores" , x = "Cond") +
scale_y_continuous (breaks = pretty_breaks(n=8)) +
theme(legend.title = element_blank()) +
theme(legend.position = "right")
我尝试过的替代代码也无法使用,包括将“show.legend = FALSE”添加到 geom_bar(); 添加“facet_wrap(~Cond)”plot.a;并在 ggplot(aes()) 中引入“fill = Temp”。最接近的解决方案是当我将 position_dodge() 参数更改为:
geom_bar (aes(fill = Temp), stat = "identity", position = position_dodge(width = .5)) +
geom_errorbar (aes(ymin = Score - SE, ymax = Score + SE, group = Cond), position = position_dodge(.5), width = .08) +
(其余代码保持不变)。这将误差条移向列的中心,但也将列移向彼此,最终使它们重叠(见附图)。
我非常感谢这方面的帮助。
谢谢!