在 ggplot2 中使用 geom_errorbar 时遇到一个常见问题。
误差线不在范围内,但在这里无关紧要。
我的问题是 geom_errorbar 正在绘制相同数据的置信区间,具体取决于使用它绘制的其他数据。
下面的代码过滤数据,仅在未注释的 SE 和 AggBar 中传递 Audio1 等于“300SW”或“3500MFL”的数据。
SE<-c(0.0861829641865964, 0.0296894376485468, 0.0323219002250762,
0.0937013798013447)
AggBar <- structure(list(Report = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("One Flash", "Two Flashes"), class = "factor"),
Visual = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("one",
"two"), class = "factor"), Audio = c("300SW", "300SW", "300SW",
"300SW", "3500MFL3500CL", "3500MFL3500CL", "3500MFL3500CL",
"3500MFL3500CL"), Prob = c(0.938828282828283, 0.0611717171717172,
0.754141414141414, 0.245858585858586, 0.534484848484848,
0.465515151515151, 0.0830909090909091, 0.916909090909091)), .Names = c("Report",
"Visual", "Audio", "Prob"), row.names = c(NA, -8L), class = "data.frame")
#SE<-c(0.0310069159026252, 0.113219880555153, 0.0861829641865964, 0.0296894376485468)
#AggBar <- structure(list(Report = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
#2L), .Label = c("One Flash", "Two Flashes"), class = "factor"),
#Visual = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("one",
#"two"), class = "factor"), Audio = c("300MFL300CL", "300MFL300CL",
#"300MFL300CL", "300MFL300CL", "300SW", "300SW", "300SW",
#"300SW"), Prob = c(0.562242424242424, 0.437757575757576,
#0.0921010101010101, 0.90789898989899, 0.938828282828283,
#0.0611717171717172, 0.754141414141414, 0.245858585858586)), .Names = c("Report",
#"Visual", "Audio", "Prob"), row.names = c(NA, -8L), class = "data.frame")
prob.bar = ggplot(AggBar, aes(x = Report, y = Prob, fill = Report)) + theme_bw() #+ facet_grid(Audio~Visual)
prob.bar + #This changes all panels' colour
geom_bar(position=position_dodge(.9), stat="identity", colour="black", width=0.8)+
theme(legend.position = "none") + labs(x="Report", y="Probability of Report", title = expression("Visual Condition")) + scale_fill_grey() +
scale_fill_grey(start=.4) +
scale_y_continuous(limits = c(0, 1), breaks = (seq(0,1,by = .25)))+
facet_grid(Audio ~ Visual)+
geom_errorbar(aes(ymin=Prob-SE, ymax=Prob+SE),
width=.1, # Width of the error bars
position=position_dodge(.09))
这将产生以下输出:
Audio1 变量显示在最右侧的垂直标签上。
但是,如果我过滤它只通过 Audio1 等于“300SW”或“300MFL”(注释的 SE 和 AggBar)的地方,“300SW 变化”的错误栏:
这次在最右边的垂直标签上可以看到 Audio1 变量,底部有“300SW”。
此更改是不正确的,因为当我仅绘制 Audio1“300SW”时,误差条与原始图匹配。
我尝试使用此处未提供的其他变量绘制 Audio1“300SW”,并且只有在使用“300MFL”时才会发生这种变化。
如果您查看 SE 变量的内容,您会发现两个版本的代码中“300SW”的值都没有变化。然而输出不同。
我无法理解这里发生了什么。欢迎任何想法或建议。
非常感谢你花时间陪伴。
下面的@Antonios K 强调了当“300SW”位于网格顶部时,错误条被正确绘制。我猜错误条与条不正确匹配,尽管我不知道为什么会这样。