如何使用ggplot2向条形图中的条形添加频率计数标签? [英] How to add frequency count labels to the bars in a bar graph using ggplot2?
问题描述
我想将 [r] 因子变量的频率分布绘制为条形图,其中条形表示因子水平的频率计数.我使用 ggplot2 来做到这一点,这没有问题.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
我不知道如何将频率计数标签添加到条形图中的条形图上.我试过的语法如下:
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
我想我在 stackoverflow 和 W.Chang 的R Graphics Cookbook"中进行了彻底的搜索,但是在上面的 geom_text() 的美学中,我找不到关于我应该将什么参数与y"匹配的任何具体答案.我尝试了一些变体,例如:(y = ..count..) 但它没有用.
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
我将不胜感激.谢谢...
I would appreciate any help. Thanks...
推荐答案
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)
这篇关于如何使用ggplot2向条形图中的条形添加频率计数标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!