2017-02-23 61 views
0

我用下面的代碼創建了一個分組的條形圖。我想將錯誤欄的寬度調整爲當前寬度的一半。但是,width = 0.5對結果沒有影響。我應該怎麼做?如何調整用stat_summary_bin創建的錯誤欄的寬度?

library(ggplot2) 
# ggplot2_2.2.1 

df <- iris 
set.seed(1) 
df$group <- sample(c('I', 'II'), nrow(df), replace = T) 

ggplot(df, aes(x = group, y = Sepal.Width, fill = Species)) + 
    stat_summary_bin(fun.data = "mean_cl_normal", 
        geom = 'bar', position = 'dodge') + 
    stat_summary_bin(fun.data = "mean_cl_normal", 
        geom = 'errorbar', position = 'dodge', width = 0.5) 

enter image description here

回答

1
# ?geom_errorbar, see bottom 
# Because the bars and errorbars have different widths 
# we need to specify how wide the objects we are dodging are 

dodge <- position_dodge(width = 0.9) 

ggplot(df, aes(x = group, y = Sepal.Width, fill = Species)) + 
stat_summary(fun.data = "mean_cl_normal", geom = 'bar', position = dodge) + 
stat_summary(fun.data = "mean_cl_normal", geom = 'errorbar', position = dodge, width = 0.5) 
+0

雖然這產生我想要的形象,這不適合'stat_summary_bin'工作。你有什麼主意嗎? – mt1022

+0

對不起,目前沒有,也許是別人? – Wolf