2013-04-01 29 views
4

我將如何將錯誤條定位在相應顏色條的中心?水平條形中心錯誤條(geom_errorbar)(geom_bar)

df1 <- data.frame(
    supp = c("OJ","OJ","OJ","VC","VC","VC"), 
    dose = c(0.5,1,2,0.5,1,2), 
    len = c(13.23,22.7,26.06,7.98,16.77,26.14), 
    se = c(1.41,1.27,0.84,0.87,0.8,1.52) 
) 

df1$dose <- factor(df1$dose) 
ggplot(df1, aes(x=dose, y=len, fill=supp)) +  
geom_bar(stat="identity", position=position_dodge()) + 
scale_fill_manual(name = "", values = c("deepskyblue1", "green")) + 
geom_errorbar(data = df1[1:3,], aes(ymin=len-se, ymax=len+se), width=.4, colour=c("deepskyblue1"), position=position_dodge(.9)) + 
geom_errorbar(data = df1[4:6,], aes(ymin=len-se, ymax=len+se), width=.4, colour=c("green"), position=position_dodge(.9)) 

回答

13

至於誤差條數據都位於相同的數據幀在那裏你不需要在geom_errorbar()提供論據data=,也沒有必要調用geom_errorbar()兩倍條數據。

你應該提供geom_errorbar()yminymaxaes(),也color=suppaes()將確保誤差線是根據supp值着色。要獲得與酒吧相同的顏色,請添加具有相同顏色名稱的scale_color_manual()。使用參數position=position_dodge(0.9),您可以在酒吧中心獲得錯誤欄。

ggplot(df1, aes(x=dose, y=len, fill=supp)) +  
    geom_bar(stat="identity", position=position_dodge()) + 
    scale_fill_manual(name = "", values = c("deepskyblue1", "green")) + 
    geom_errorbar(aes(ymin=len-se, ymax=len+se,color=supp), width=.4, 
     position=position_dodge(.9))+ 
    scale_color_manual(name = "", values = c("deepskyblue1", "green")) 

enter image description here