how to add data labels to geom_histogram

Hi, I try to create a histogram to show the distribution of sick days reported by our organization.

below is my code.
ggplot(data,mapping=aes(x=Annualized.Sick.Days,y=..count..,label=..count..,fill=Direct.Indirect))+
geom_histogram(binwidth=10,color="white")+
scale_x_continuous(breaks = seq(30, 100, 10), lim = c(30, 100))+
theme_classic2() +
geom_text(stat="bin", size=2,vjust=0)

Unfortunately, the labels are not in place, it looks there are more data labels than it should be.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

If the following does not solve your problem, please follow nirgrahanuk's advice and provide a reprex.
Notice I put the breaks argument in both geom_histogram and geom_text.

library(ggplot2)
DF <- data.frame(Direct.Indirect=rep(c("Direct","Indirect"),100),
                 Annualized.Sick.Days=runif(100,30,100))
ggplot(DF,mapping=aes(x=Annualized.Sick.Days,label=..count..,fill=Direct.Indirect))+
  geom_histogram(binwidth=10,color="white",breaks = seq(30, 100, 10))+
  theme_classic() +
  geom_text(stat="bin", size=2,vjust=0,breaks = seq(30, 100, 10),position="stack")

Created on 2020-11-08 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.