简体   繁体   中英

Adding Count on top of bars in GGPLOT2

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM