如何在使用 ggplot2 制作的 geom_bar 图中减少条形之间的间距而不增加其宽度?
我想减少条之间的间距,但不增加所述条的宽度。我在 ggplot2 中使用 geom_bar 函数。
几年前有人问过同样的问题,并提供了一个示例图(ggplot2:如何使用 geom_bar 减少宽度和条形之间的间距)。然而,没有人想出一个令人满意的解决方案来解决这个问题。我希望这一次会有所不同!
预先感谢大家。
拉奎尔.
I want to reduce the spacing between bars BUT without increasing the width of said bars. I am using the geom_bar function in ggplot2.
Someone asked this same question some years ago and provided an example plot (ggplot2 : How to reduce the width AND the space between bars with geom_bar). However, no one ever came up with a satisfactory solution to this problem. I hope this time will be different!
Thank you all in advance.
Raquel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们从逻辑上思考这一点,并使用以下具体示例进行演示:
为了使这一点完全可重现,让我们指定 4 英寸 x 4 英寸的绘图窗口大小:
如果我们减小酒吧但保持条本身的宽度相同,那么最左边条的左边缘和最右边条的右边缘之间的距离必然变小。实现此目的的最常见方法是增加条形的宽度,但将绘图窗口缩小相同的量。
如果我们将条形尺寸从 1/2 增加到 2/3 并将绘图宽度减小到 3 英寸,则条形宽度将与像素数完全相同,但它们之间的间距会更小:
你会请注意,情节更窄,但无论您如何实现这一点,这都是事实。
您可以保持绘图窗口大小相同,但增加绘图两侧的间距:
或者指定坐标比而不是更改绘图窗口大小:
或者在绘图中添加大边距:
但所有这些方法本质上都涉及相同的事情:如果您希望条形图靠得更近但要保持条形大小相同,您必须使图更窄。链接问题中的答案确实指出了这一点,而且我认为这并不令人满意 - 这是几何必然性。
Let's think this through logically, and use the following concrete example to demonstrate:
To make this fully reproducible, let us specify a drawing window size of 4 inches by 4 inches:
If we decrease the spacing between the bars but keep the bars themselves the same width, then it follows necessarily that the distance between the left edge of the leftmost bar and the right edge of the rightmost bar must get smaller. The most common way to achieve this would be to increase the width of the bars but shrink the plotting window by the same amount.
If we increase the bar size from 1/2 to 2/3 and decrease the plot width to 3 inches, the bar width will be exactly the same number of pixels wide but the spacing between them will be smaller:
You will notice that the plot is narrower, but that is going to be true however you achieve this.
You could instead keep the plot window the same size but increase the spacing on either side of the plot:
Or specify a co-ordinate ratio instead of changing your plot window size:
Or add large margins to the plot instead:
But all of these methods involve essentially the same thing: if you want the bars to be closer together but the bars to remain the same size, you have to make the plot narrower. The answer in the linked question does point this out, and I don't think it is unsatisfactory - it is a geometric necessity.