Doctor David Anderson
Doctor David Anderson

Reputation: 274

How can I add mean labels to a bar chart?

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.

Upvotes: 1

Views: 2505

Answers (1)

Jaap
Jaap

Reputation: 83215

It's always good to add a reproducible example to your question.

Converting my comment to an answer with the use of some example data:

# 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))

which results in:

enter image description here

Upvotes: 1

Related Questions