首页 文章

ggplot2 position = 'dodge'产生太宽的条形

提问于
浏览
3

我'm interested in producing a histogram with position='躲闪'和填充=一些因素(即每个栏/组内不同子组的并排栏),但ggplot2给了我类似the first plot here的东西,它有一个太宽的最右边的栏并且没有为空留空间小组,我想 .

这是一个简单的案例:

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")

我如何实现我想要的目标?提前致谢 .

1 回答

  • 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

相关问题