0

I was plotting a bar chart and was wondering how would I add the count on top of the bars

ggplot(data = sf_crime)+
  geom_bar(aes(x=day_of_week, y=..prop..,group=1), stat = "count", fill ="forestgreen", color = "gold") +
  geom_text()

I was looking at the proportion of crimes on each weekday, and wanted to add the count on top of each bar but was having a lot of issues. I know we would use the geom_text function to do so.

Andyy Hu
  • 69
  • 3

1 Answers1

2

You could achieve your desired result by setting the stats layer for geom_text to stat="count" and by mapping the computed count on the label aes:

As your provided no data I make use of mtcars:

library(ggplot2)

ggplot(mtcars, aes(cyl)) +
  geom_bar(aes(y = ..prop..), fill ="forestgreen", color = "gold") +
  geom_text(aes(y = ..prop.., label = ..count..), stat = "count", vjust = 0, nudge_y = .01)

stefan
  • 90,330
  • 6
  • 25
  • 51