0

在 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 变量显示在最右侧的垂直标签上。

但是,如果我过滤它只通过 Audio1 等于“300SW”或“300MFL”(注释的 SE 和 AggBar)的地方,“300SW 变化”的错误栏:

在此处输入图像描述

这次在最右边的垂直标签上可以看到 Audio1 变量,底部有“300SW”。

此更改是不正确的,因为当我仅绘制 Audio1“300SW”时,误差条与原始图匹配。

我尝试使用此处未提供的其他变量绘制 Audio1“300SW”,并且只有在使用“300MFL”时才会发生这种变化。

如果您查看 SE 变量的内容,您会发现两个版本的代码中“300SW”的值都没有变化。然而输出不同。

我无法理解这里发生了什么。欢迎任何想法或建议。

非常感谢你花时间陪伴。

下面的@Antonios K 强调了当“300SW”位于网格顶部时,错误条被正确绘制。我猜错误条与条不正确匹配,尽管我不知道为什么会这样。

4

2 回答 2

3

问题是它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)
于 2015-08-06T12:30:01.153 回答
1

绘制误差线的代码是:

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")]
于 2015-08-06T11:08:06.213 回答