2015-12-21 28 views
0

我發現很多問題都會在ggplot2中處理重新定位錯誤條,但沒有一個具有我特殊問題。我希望這不是重複的!在一個多面條形圖上重新定位geom_errorbar

我有一個多面的barplot,我試圖在已經計算出的置信區間添加錯誤欄。參數statposition似乎沒有任何影響,不管它們是在aes()參數中還是僅在geom_errorbar()。以下是我與合作:

> dput(anthro.bp) 
structure(list(Source = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("PED", "RES"), class = "factor"), 
Response = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D" 
), class = "factor"), Value = c(0.5043315, 0.03813694, 0.20757498, 
0.249956615, 0.9232598, 0.0142572, 0.0537258, 0.008757155, 
0.897265, 0.03153401, 0.06610772, 0.005093254, 0.8360081, 
0.03893782, 0.0370325, 0.088021559), Distance = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L 
), .Label = c("Near", "Far"), class = "factor"), UCI = c(0.5853133, 
0.07247573, 0.27357566, 0.32335335, 0.9744858, 0.03844421, 
0.08841988, 0.04262752, 0.9422062, 0.0540748, 0.09600908, 
0.03348959, 1.2445932, 0.11196198, 0.10133358, 0.52272511 
), LCI = c(0.4233497, 0.003798153, 0.1415743, 0.17655988, 
0.8720338, -0.009929805, 0.01903172, -0.02511321, 0.8523238, 
0.008993231, 0.03620636, -0.02330308, 0.427423, -0.034086335, 
-0.02726858, -0.34668199)), .Names = c("Source", "Response", 
"Value", "Distance", "UCI", "LCI"), row.names = c(NA, -16L), class = "data.frame") 

anthro.bp[,4]<-factor(anthro.bp[,4], levels=c("Near","Far")) 
bp <- ggplot(anthro.bp, aes(Value, fill=Response)) 
bp + geom_bar(aes(x=Source,y=Value), stat="identity", position="dodge") + 
    geom_errorbar(aes(ymin=LCI,ymax=UCI), stat="identity", position="dodge",width=0.25) + 
    facet_wrap(~Distance) + 
    labs(x="Disturbance Source", y="Mean Probability") 

我也試圖在geom_errorbar()命令都距離aes()參數外的,要再次使用position=position_dodge(width=1)。我的圖表如下鏈接(我沒有足夠的聲望來嵌入圖像,但道歉!)。

Faceted bar graph with incorrectly positioned error bars

我也越來越兩個錯誤消息:

警告消息:
1:在loop_apply(N,do.ply):
position_dodge需要非重疊x上間隔
2:在loop_apply(n,do.ply)中:
position_dodge要求非重疊的x間隔

這是我第一次在教室環境之外使用ggplot2,因此非常鼓勵建設性的批評。

回答

2

由於某些我不清楚的原因,ggplot2是通過不同的值來避開你的條和誤差條。我通過手動指定了閃避寬度來解決這個問題。此外,您已將y和x美學設置爲geom_bar。注意他們現在放置在哪裏。最後,geom_errorbar調用不需要stat ='identity'。

bp <- ggplot(anthro.bp, aes(x=Source,y=Value, fill=Response)) 
bp + geom_bar(stat="identity", position = position_dodge(width = 0.90)) + 
    geom_errorbar(aes(ymin=LCI,ymax=UCI), position = position_dodge(width = 0.90),width=0.25) + 
    facet_wrap(~Distance) + 
    labs(x="Disturbance Source", y="Mean Probability") 
0
根據 How to make dodge in geom_bar agree with dodge in geom_errorbar, geom_point

是你想要的嗎?

anthro.bp$dmin <- anthro.bp$Value - anthro.bp$LCI 
anthro.bp$dmax <- anthro.bp$UCI - anthro.bp$Value 

ggplot(data=anthro.bp, aes(x=Source, ymin=Value-dmin, ymax=Value+dmax, fill=Response)) + 
geom_bar(position=position_dodge(), aes(y=Value), stat="identity") + 
geom_errorbar(position=position_dodge(width=0.9), colour="black") + facet_wrap(~Distance) + labs(x="Disturbance Source", y="Mean Probability") 

enter image description here

+0

正確的主意,但計算錯誤欄的每條腿似乎增加了一個額外的步驟。 – Maalthou

0

我相信你的主要問題是在BP的AES未定義的x值(來源)。

bp <- ggplot(anthro.bp, aes(x=Source,y=Value, fill=Response)) 
bp + geom_bar(stat="identity", position=position_dodge()) + 
geom_errorbar(aes(ymin=LCI,ymax=UCI),width=0.25, stat="identity", position=position_dodge(0.9)) + 
facet_wrap(~Distance) + 
labs(x="Disturbance Source", y="Mean Probability") 
相關問題