2017-02-23 59 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