如何在条形图中添加平均标签?

时间:2015-11-13 06:53:15

标签: r ggplot2

我想在R的条形图底部添加每个条件的平均值。最终产品在excel中看起来像这样(注意每个条形图底部显示的方法): enter image description here

我目前的代码如下:

pmrtBar <- ggplot(reslagdfClean,aes(x=Condition, y=PMMissProp*100)) + stat_summary(fun.y = mean, geom = "bar", fill = cbPalette) +
  theme(axis.title=element_text(size=12)) +
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=.1, size = .25) +
  coord_cartesian(ylim=c(0,50)) + 
  labs(x = "Condition", y="Mean Mean Miss Proportion (%)") +
  apatheme
pmrtBar                  

我是R环境的新手。任何有关上述代码的反馈也值得赞赏。

1 个答案:

答案 0 :(得分:1)

在您的问题中添加reproducible example总是好的。

使用一些示例数据将我的评论转换为答案:

# example data
dat <- data.frame(id = c("ACT","Blank","None"),
                  mn = c(0.3833,0.38,0.4033),
                  se = c(0.1,0.15,0.12))

# creating the plot
ggplot(dat, aes(x=id, y=mn, fill=id)) +
  geom_bar(stat="identity", width=0.7) +
  geom_errorbar(aes(ymax = mn + se, ymin = mn - se), width=0.25) +
  geom_text(aes(y = 0.2, label = paste(mn*100, "%"))) +
  labs(x = "\nCondition", y = "Proportion (%)\n") +
  scale_y_continuous(breaks = seq(0.15, 0.55, 0.05), labels = scales::percent) +
  coord_cartesian(ylim = c(0.15,0.55)) +
  theme_minimal(base_size = 14) +
  theme(panel.grid.major.y = element_line(linetype = 2, color = "grey80", size = 1))

导致:

enter image description here