Solved – How to add mean labels to a bar chart in [R]?

r

I would like to add the mean of each condition at the base of my bar chart in R. The final product looks something like this in excel (note the means are displayed at the base of each bar):
enter image description here

My current code is as follows:

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                  

I am new the R environment. Any feedback on the code above is also appreciated.

Best Answer

Since you didn't provide a reproducible dataset, I will use ToothGrowth. First, create a new data frame containing the mean values for each group.

means <- aggregate(len ~ supp, ToothGrowth, mean)
#   supp      len
# 1   OJ 20.66333
# 2   VC 16.96333

This data frame can be used for ggplot. You can place text with geom_text.

library(ggplot2)
ggplot(means, aes(x = supp, fill = supp, y = len)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = format(len, digits = 4), y = 0.7))

enter image description here

Related Question