2011-08-18 57 views
3

我有興趣生成位置='dodge'和fill =一些因子的直方圖(即每個條形圖/組中不同子組的並排條形圖),但ggplot2給了我一些像the first plot here,它有一個最右邊的酒吧,太寬,並保留沒有空的空組,我想。ggplot2 position ='dodge'產生太寬的條紋

這裏有一個簡單的例子:

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 - bars too wide我有這個想法,而它在技術上產生相同寬度的條,但保留爲空組沒有空間:

ggplot(df, aes(x=a, fill=a))+ 
geom_bar(aes(y=..count../sum(..count..))) + 
facet_grid(~b,scales="free",space="free") 

我如何實現我想要的?提前致謝。

回答

3

ggplot中的默認選項產生我認爲你所描述的內容。 scales="free"space="free"選項與您想要的相反,因此只需從代碼中移除這些選項即可。此外,geom_bar的默認stat將通過計數進行聚合,因此您不必明確指定您的統計信息。

ggplot(df, aes(x=a, fill=a)) + geom_bar() + facet_grid(~b) 

enter image description here