-3

I figured out how to get the count of each bin from ggplot, does anyone know how to show these numbers on the plot?

g <- ggplot()+geom_histogram()
ggplot_build(g)$data[[1]]$count
Jaap
  • 77,147
  • 31
  • 174
  • 185
PhoenixQ
  • 103
  • 1
  • 2
  • 5
  • Use `stat_bin` with `geom_text`. – Roland May 20 '14 at 19:46
  • Welcome to StackOverflow! Please read the info about how to [ask a question](http://stackoverflow.com/help/how-to-ask) and how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Jaap May 20 '14 at 20:06

1 Answers1

18

You can add a stat_bin to do the calculations of counts for you, and then use those values for the labels and heights. Here's an example

set.seed(15)
dd<-data.frame(x=rnorm(100,5,2))
ggplot(dd, aes(x=x))+ geom_histogram() +
    stat_bin(aes(y=..count.., label=..count..), geom="text", vjust=-.5) 

labeled histogram

MrFlick
  • 178,638
  • 15
  • 253
  • 268