简体   繁体   English

在 GGPLOT2 中的条形顶部添加计数

[英]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.我知道我们会使用 geom_text 函数来做到这一点。

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:您可以通过将geom_text的 stats 层设置为stat="count"并通过将计算的count映射到label aes 来实现您想要的结果:

As your provided no data I make use of mtcars :由于您未提供任何数据,我使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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