ggplot2 position ='dodge'产生的条块太宽 [英] ggplot2 position='dodge' producing bars that are too wide
问题描述
我有兴趣制作一个位置='dodge'和fill =一个因子的柱状图(即每个柱/组内的不同子组并排排列),但ggplot2给了我一些像这是第一个情节,它有一个最右边的酒吧,它太宽了,并且没有空余空间,我想要的。
以下是一个简单的例子:
df = data.frame(a = c('o','x','o','o'),b = c('a','b','a','b'))
qplot(a,data = df,fill = b,position ='dodge')
一个href =https://stackoverflow.com/questions/2708994/ggplot-geom-bar-bars-too-wide> ggplot geom_bar - 酒吧太宽了我有这个想法,虽然它技术上生产bar,但没有为空组保留空间:
ggplot(df,aes(x = a,fill = a))+
geom_bar(aes(y = .. count ../ sum(.. c ount ..)))+
facet_grid(〜b,scales =free,space =free)
我如何实现我想要的?
在 ggplot
中的默认选项产生了我想你描述一下。 scales =free
和 space =free
options与你想要的相反,所以简单地删除这些来自代码。此外, geom_bar
的默认 stat
将通过计数进行聚合,因此您不必明确指定您的统计信息。
pre $ g $ p $ ggplot(df,aes(x = a,fill = a))+ geom_bar()+ facet_grid(〜b)
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.
The default options in ggplot
produces what I think you describe. The scales="free"
and space="free"
options does the opposite of what you want, so simply remove these from the code. Also, the default stat
for geom_bar
is to aggregate by counting, so you don't have to specify your stat explicitly.
ggplot(df, aes(x=a, fill=a)) + geom_bar() + facet_grid(~b)
这篇关于ggplot2 position ='dodge'产生的条块太宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!