Search code examples
rggplot2histogram

How can I add count label in ggplot2 histogram when using breaks?


I am trying to add count labels to a histogram created with ggplot. It works perfectly fine if I use default bins but if I use breaks to define the bars, the labels will be calculated and positioned as if I was using default bins. How can I choose the counts to be calculated from the breaks I assinged? I found some answers regarding using bins instead of breaks, but they all seem to have the issue that the first bar will not be from x=0 to x=20. It doesn't matter to me if the answer uses bins or breaks, but I want the first bar to have values between x=0 and x=20, and the rest of the bars be of same width. The data I am working with doesn't have negative values but it has values starting from 0 upwards.

This is how to reproduce the problem:

library(tidyverse)
d <- cars

ggplot(d, aes(x = dist)) + 
  geom_histogram(color="black", fill="gray", breaks=c(0,20,40,60,80,100,120)) +
  scale_x_continuous(breaks=c(0,20,40,60,80,100,120)) +
  geom_text(stat='count', aes(label=..count..), vjust=-1)

And this is the outcome:

enter image description here

I have also used stat_count(bins=6, geom="text",aes(label=..count..)) instead of geom_text(), but it gives the same result: it calculates the stats for 30 bins.


Solution

  • Switch to stat="bin" in geom_text to compute the appropriate counts and use the same breaks as for geom_histogram:

    library(ggplot2)
    
    d <- cars
    
    breaks <- c(0, 20, 40, 60, 80, 100, 120)
    
    ggplot(d, aes(x = dist)) +
      geom_histogram(color = "black", fill = "gray", breaks = breaks) +
      scale_x_continuous(breaks = breaks) +
      geom_text(
        stat = "bin", aes(label = after_stat(count)),
        vjust = -1, breaks = breaks
      )