手动为条形图添加置信区间

时间:2019-01-11 01:07:53

标签: r ggplot2 confidence-interval

对于物种“ x”和“ y”的每个平均值,我必须遵循具有置信区间“ ci_min”和ci_max的数据框。如何在条形图上手动添加置信区间?

data <- data.frame("sp" = c("x","y"), count = c(-4.011, 2.45), "ci_min" = c(-4.2,1.68), "ci_max" = c(-4.01, 3.28))

library(ggplot2)
ggplot(data, aes(x = sp, y = counts, fill = sp)) +
    stat_summary(geom="bar", fun.y=mean, position = "dodge") 

1 个答案:

答案 0 :(得分:1)

您可以通过在主geom_errobar调用中指定yminymax来添加带有ggplot2的错误栏。

ggplot(data, aes(sp, count, ymin = ci_min, ymax = ci_max, fill = sp)) + 
    geom_bar(stat = "identity") +
    geom_errorbar()

enter image description here