在ggplot2中显示频率和barplot [英] Show frequencies along with barplot in ggplot2

查看:192
本文介绍了在ggplot2中显示频率和barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在barplot内显示频率...呃,我想要它们在某处图表中:在酒吧下方,酒吧内,酒吧上方或图例区域中。我记得(我可能是错的),它可以在 ggplot2 中完成。这可能很简单...至少看起来很简单。代码如下:

pre $ $ $ $ $ $ $ pre $ p $ ()

有没有可能让频率嵌入图表中?

解决方案

geom_text 文本 from base graphics:

  p + geom_bar()+ stat_bin(aes(label = .. count ..),vjust = 0,
geom =text,position =identity)

要调整标签的y位置,您可以在 stat_bin 中使用 y = 审美性:例如, y = .. count .. + 1 会将标签放在一个单位上方。



上面还有如果您在内部使用 geom_text stat =bin,则可以使用。


I'm trying to display frequencies within barplot ... well, I want them somewhere in the graph: under the bars, within bars, above bars or in the legend area. And I recall (I may be wrong) that it can be done in ggplot2. This is probably an easy one... at least it seems easy. Here's the code:

p <- ggplot(mtcars)
p + aes(factor(cyl)) + geom_bar()

Is there any chance that I can get frequencies embedded in the graph?

解决方案

geom_text is tha analog of text from base graphics:

p + geom_bar() + stat_bin(aes(label=..count..), vjust=0, 
                          geom="text", position="identity")

If you want to adjust the y-position of the labels, you can use the y= aesthetic within stat_bin: for example, y=..count..+1 will put the label one unit above the bar.

The above also works if you use geom_text and stat="bin" inside.

这篇关于在ggplot2中显示频率和barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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