Search code examples
rggplot2histogram

How to display the mean (in text) for a histogram?


I want to add the mean value. geom_text(aes(mean(rnorm)) and geom_label(aes(mean(rnorm)) did not do the job.

The mean should be displayed for each of the species (sp)

set.seed(111)
rand.data <- rnorm(1000,5,1)
sp <- rep(c("A","B","C","D"), each =250)
rand.df <- data.frame(rand.data,sp)

rand.df$sp <- as.factor(rand.df$sp)

ggplot(rand.df, aes(x=rand.data), color = sp) + geom_histogram() +
  theme_classic() + geom_vline(aes(xintercept=mean(rand.data)), color="blue", linetype="dashed", size=1) + facet_grid(.~sp)

enter image description here

Edit: I also notice that the geom_vline() as written here does not show the means broken down by sp, but rather displays the overall mean

rand.df %>% 
  group_by(sp)%>% 
  dplyr::summarise(rand.df.mean = mean(rand.data))

Solution

  • Here's a faceted alternative:

    library(dplyr)
    means <- rand.df %>%
      group_by(sp) %>%
      summarize(rand.mean = round(mean(rand.data), 2))
    ggplot(rand.df, aes(x=rand.data)) +
      geom_histogram(aes(color = sp)) +
      theme_classic() +
      geom_vline(aes(xintercept = rand.mean), data = means, color="blue", linetype="dashed", linewidth=1) +
      geom_label(aes(x = rand.mean, label = rand.mean), y = Inf, data = means, vjust = 1.5) +
      facet_grid(.~sp)
    

    ggplot2 with colored and labeled histograms

    Notes:

    • ggplot2 prefers linewidth= to size= for lines
    • your initial use of color=sp was outside the aes(.), so it was interpreted as a static (non-varying); moving it inside the aes makes it do what I think you intended; in fact, I moved it within the geom_histogram since it would otherwise apply to all geoms (you can do that if you want)
    • I chose color= for the histograms, you may also prefer fill=sp instead or in addition (I think fill= makes more sense)