[英]Adding Count on top of bars in GGPLOT2
我正在绘制条形图,想知道如何在条形顶部添加计数
ggplot(data = sf_crime)+
geom_bar(aes(x=day_of_week, y=..prop..,group=1), stat = "count", fill ="forestgreen", color = "gold") +
geom_text()
我正在查看每个工作日的犯罪比例,并想在每个条形顶部添加计数,但遇到了很多问题。 我知道我们会使用 geom_text 函数来做到这一点。
您可以通过将geom_text
的 stats 层设置为stat="count"
并通过将计算的count
映射到label
aes 来实现您想要的结果:
由于您未提供任何数据,我使用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.