ggplot2 位置='躲闪'生产太宽的条形
我有兴趣生成一个带有position='dodge'和fill=some Factor的直方图(即每个条/组内不同子组的并排条),但是ggplot2给了我类似这里的第一个图,它的最右边的条形太宽并且没有为空组保留空间,这 我想。
这是一个简单的案例:
df = data.frame(a=c('o','x','o','o'), b=c('a','b','a','b'))
qplot(a, data=df, fill=b, position='dodge')
从 ggplot geom_bar - 条形太宽我得到了这个想法,虽然它在技术上产生了相同宽度的条形,但没有为空组保留空间:
ggplot(df, aes(x=a, fill=a))+
geom_bar(aes(y=..count../sum(..count..))) +
facet_grid(~b,scales="free",space="free")
我如何实现我想要的?提前致谢。
I'm interested in producing a histogram with position='dodge' and fill=some factor (i.e. side-by-side bars for different subgroups within each bar/group), but ggplot2 gives me something like the first plot here, which has a rightmost bar that's too wide and reserves no space for the empty group, which I would like.
Here's a simple case:
df = data.frame(a=c('o','x','o','o'), b=c('a','b','a','b'))
qplot(a, data=df, fill=b, position='dodge')
From ggplot geom_bar - bars too wide I got this idea, and while it technically produces a bar of the same width, but preserves no space for the empty group:
ggplot(df, aes(x=a, fill=a))+
geom_bar(aes(y=..count../sum(..count..))) +
facet_grid(~b,scales="free",space="free")
How do I achieve what I want? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ggplot 中的默认选项会生成我认为您所描述的内容。
scales="free"
和space="free"
选项的作用与您想要的相反,因此只需从代码中删除它们即可。此外,geom_bar
的默认stat
是通过计数进行聚合,因此您不必显式指定统计数据。The default options in
ggplot
produces what I think you describe. Thescales="free"
andspace="free"
options does the opposite of what you want, so simply remove these from the code. Also, the defaultstat
forgeom_bar
is to aggregate by counting, so you don't have to specify your stat explicitly.