如何减少ggplot2中堆积条形图的条形间隙

时间:2015-05-11 05:02:56

标签: r ggplot2 bar-chart

我想减少条间隙并保持堆积条形图的条宽。

堆积条形图:

((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);

((ActionBarActivity)getActivity()).getSupportActionBar().setIcon(R.drawable.my_icon);

enter image description here

然后我想通过p <- ggplot(dat, aes(x = plant, y = percentage*100, fill = group)) + geom_bar(stat = "identity", width =0.20) 更改条形差距:

position=position_dodge(0.9)

此解决方案可以更改条形间隙,但条形图未拆开。那么如何改变条形间隙并保持条形宽度和堆叠?提前谢谢!

enter image description here

我的数据:

p <- ggplot(dat, aes(x = plant, y = percentage*100, fill = group)) + 
  geom_bar(stat = "identity", position=position_dodge(0.9),width =0.20)

1 个答案:

答案 0 :(得分:0)

position = position_dodge用于并排显示条形图的填充部分。我无法特别找到解决方案。但是,当我遇到这样的问题时,我会调整整个图形的宽度来调整条形的宽度。请考虑以下示例,并查看调整宽度时保存的图形。希望这可以帮助。关键是如果你使条形宽度变窄并减小条形之间的空间,最终你的图形宽度会减小。

ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
ggsave(filename = "trial1.png",plot = P,width=15,height = 10)

enter image description here

ggsave(filename = "trial2.png",plot = P,width=5,height = 10)

enter image description here