geom_errorbar表现异常,ggplot2 [英] geom_errorbar behaving strangely, ggplot2

查看:420
本文介绍了geom_errorbar表现异常,ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot2中使用geom_errorbar时,我经常遇到问题.

I have an usual problem when using geom_errorbar in ggplot2.

误差线不在范围内,但是在此无关紧要.

The error bars are not within range but that is of no concern here.

我的问题是,geom_errorbar会不同地绘制同一数据的置信区间,具体取决于所绘制的其他数据.

My problem is that geom_errorbar is plotting the confidence intervals for the same data differently depending on what other data is plotted with it.

下面的代码仅过滤未注释的SE和AggBar中Audio1等于"300SW"或"3500MFL"的数据.

The code below filters the data only passing data where Audio1 is equal to "300SW" OR "3500MFL" in the uncommented SE and AggBar.

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

The Audio1 variables are seen on the rightmost vertical labels.

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

However if I filter where it only passes where Audio1 is equal to "300SW" OR "300MFL" (the commented SE and AggBar) the error bars for "300SW change":

这次在最右边的垂直标签上看到Audio1变量,在底部带有"300SW".

The Audio1 variables are seen on the rightmost vertical labels with "300SW" on the bottom this time.

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

This change is the incorrect one because when I plot just the Audio1 "300SW" the error bars match the original plot.

我已经尝试绘制Audio1"300SW"以及此处未显示的其他变量,并且当出现"300MFL"时会发生这种变化.

I have tried plotting the Audio1 "300SW" with other variables not presented here and it is only when presenting with "300MFL" that this change occurs.

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

If you look at the SE variable contents you will see that there is no change in the values therein for "300SW" in both versions of the code. Yet the outputs differ.

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

I cannot fathom what is happening here. Any ideas or suggestions are welcome.

非常感谢您的时间.

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

@Antonios K below has highlighted that when "300SW" is on top of the grid the error bars are correctly drawn. I'm guessing that the error bars are being incorrectly matched to the bars although I don't know why this is the case.

推荐答案

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

The problem is that SE is not stored inside the data frame: it's just floating around in the global environment. When the data is facetted (which involves rearranging the order), it no longer lines up with the correct records. Fix the problem by storing SE in the data frame:

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表现异常,ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆