Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To include a count label in a ggplot2 histogram that employs breaks, you need to use the geom_text function with the stat argument set to count. Here is an example code:

library(ggplot2)
data(mtcars)

ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, color = "black", fill = "white") +
  geom_text(stat = "count", aes(label = ..count..), vjust = -0.5)

In this code, geom_histogram creates the histogram with specified binwidth, color and fill. geom_text adds the count labels to the histogram bars using the stat = "count" argument that counts the number of observations in each bin. The aes(label = ..count..) argument specifies to use the count as the label for each bar, while vjust adjusts the text position vertically.