如何使用ggplot2将频率计数标签添加到条形图中的条形图? [英] How to add frequency count labels to the bars in a bar graph using ggplot2?

查看:1199
本文介绍了如何使用ggplot2将频率计数标签添加到条形图中的条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制[r]因子变量的频率分布作为条形图,其中条形图表示因子水平的频率计数。我使用ggplot2来做到这一点,并没有任何问题。



我无法弄清楚如何在条形图中为条形图添加频率计数标签。我试过的语法如下:

  ggplot(data,aes(x = factorvar))+ geom_bar(fill =somecolor)+ geom_text(aes(y = ???))

在由W.Chang编写的stackoverflow和R Graphics Cookbook中搜索,但是我无法找到任何具体的答案,我应该在上面的geom_text()的美学中匹配y的参数。我尝试了一些变体,如:(y = ..count ..),但它没有奏效。



我很感激任何帮助。谢谢...

解决方案

ggplot(data = diamonds,aes(x = clarity) )+
geom_bar()+
geom_text(stat ='count',aes(label = .. count ..),vjust = -1)


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 = ???))

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆