ggplot2:如何使用geom_bar减小宽度和条之间的间隔 [英] ggplot2 : How to reduce the width AND the space between bars with geom_bar

查看:1206
本文介绍了ggplot2:如何使用geom_bar减小宽度和条之间的间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以使用 width 参数来更改 geom_bar 中条形的宽度。确实可行,但随后会在条之间产生更大的间隙。有没有办法手动将拉杆推得更近些?我应该以某种方式操纵轴吗?



这里是一个示例,将右侧的宽度更改为0.3以获取所需的条形宽度。

 库(tidyverse)
库(gridExtra)

p1<-ggplot(iris,aes(Species,Petal) ..Length))+ geom_bar(stat = summary)
p2<-ggplot(iris,aes(Species,Petal.Length))+ geom_bar(stat = summary,width = 0.3)
grid.arrange(p1,p2,nrow = 1)



注意:我知道这个问题与此类似,但是缩小差距的答案并不明显。




I understand that one can change the width of a bar in geom_bar using the width argument. That does work, but then it creates a larger gap between the bars. Is there a way to manually push the bars closer to together? Should I be manipulating the axis somehow instead?

Here is an example, changing width to 0.3 on the right to get the desired bar width.

library(tidyverse)
library(gridExtra)

p1 <- ggplot(iris, aes(Species, Petal.Length)) + geom_bar(stat="summary")
p2 <- ggplot(iris, aes(Species, Petal.Length)) + geom_bar(stat="summary", width=0.3)
grid.arrange(p1,p2,nrow=1)

Note: I know this question is similar to this one, but the answer to closing the gap was not apparent.

How to change the space between bars in geom_bar?

解决方案

I would adjust the plot's aspect ratio, and have ggplot automatically assign the right width for the bars and the gap between them:

  ggplot(iris, aes(Species, Petal.Length)) + 
      geom_bar(stat="summary", width=0.4) +
      theme(aspect.ratio = 2/1)

Produces this:

这篇关于ggplot2:如何使用geom_bar减小宽度和条之间的间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆