繁体   English   中英

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 =“ 300SW”或“ 3500MFL”过滤时输出

在最右边的垂直标签上可以看到Audio1变量。

但是,如果我仅在Audio1等于“ 300SW”或“ 300MFL”(注释为SE和AggBar)的地方过滤通过的地方,则“ 300SW更改”的错误栏如下:

在此处输入图片说明

这次,Audio1变量显示在最右边的垂直标签上,底部带有“ 300SW”。

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

我尝试绘制Audio1“ 300SW”以及此处未显示的其他变量,并且只有在显示“ 300MFL”时才会发生此更改。

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

我无法理解这里发生的事情。 欢迎任何想法或建议。

非常感谢你花时间陪伴。

下面的@Antonios K突出显示,当“ 300SW”位于网格顶部时,将正确绘制错误栏。 我猜测错误条与错误条的匹配不正确,尽管我不知道为什么会这样。

问题在于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)

绘制误差线的代码位是:

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结合使用时,请尝试将300SW替换为1_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")]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM