geom_errorbar 表现异常,ggplot2

geom_errorbar behaving strangely, ggplot2

我在 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”位于网格顶部时,误差线被正确绘制。我猜误差条与条的匹配不正确,尽管我不知道为什么会这样。

绘制误差线的代码位是:

geom_errorbar(aes(ymin=Prob-SE, ymax=Prob+SE), width=.1, # Width of the error bars position=position_dodge(.09))

所以,我猜它是有东西的。 正如您所说,SE 变量在两种情况下都相同,但您绘制的是 Prob-SE 和 Prob+SE。如果您执行 AggBar$Prob-SE 和 AggBar$Prob+SE,每种情况下您将获得 300SW 的不同值。

可能与您的 Audio1 值的顺序有关。其他有效的案例是不是在地块的顶部也有 300SW?

尝试

sort(unique(DataRearrange$Audio1) )

[1] "300MFL"  "300SW"   "3500MFL"

结合前两个将为您在图的底部提供 300SW。 结合最后两个会给你顶部的 300SW。

因此,为了检查这个假设,在您组合 300MFL 和 300SW 的第二种情况下,尝试用 1_300SW 替换 300SW(这样 300SW 将被绘制在顶部),看看会发生什么。只是做:

    DataRearrange$Audio1[DataRearrange$Audio1=="300SW"] = "1_300SW"

# Below is the alternative coupling..

ErrorBarsDF <- DataRearrange[(DataRearrange$Audio1=="1_300SW" | DataRearrange$Audio1=="300MFL"), c("correct","Visual1", "Audio1", "Audio2","correct_response", "response", "subject_nr")]
DataRearrange <- DataRearrange[(DataRearrange$Audio1=="1_300SW" | DataRearrange$Audio1=="300MFL"), c("correct","Visual1", "Audio1", "Audio2","correct_response", "response", "subject_nr")]

问题是 SE 没有存储在数据框内:它只是在全局环境中浮动。当数据被分面(涉及重新排列顺序)时,它不再与正确的记录对齐。通过在数据框中存储 SE 来解决问题:

AggBar$SE <- c(0.0310069159026252, 0.113219880555153, 0.0861829641865964, 0.0296894376485468)

ggplot(AggBar, aes(Report, Prob, Report)) +
  geom_bar(stat = "identity", fill = "grey50") +
  geom_errorbar(aes(ymin = Prob - SE, ymax = Prob + SE), width = 0.4) + 
  facet_grid(Audio ~ Visual)