如何调整stat_summary_bin创建的错误栏的宽度?

时间:2017-02-23 14:37:45

标签: r ggplot2

我使用以下代码创建了一个分组的条形图。我想将errorbar的宽度调整为一半 当前宽度。但是,width = 0.5对结果没有影响。我该怎么做?

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 个答案:

答案 0 :(得分: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)