简体   繁体   English

在 r ggplot 中,是否可以调整 x 轴上箱线图之间的间距?

[英]In r ggplot, is it possible to adjust the spacing between boxplots on the x axis?

Say you want a simple boxplot, without any subgroups:假设您想要一个没有任何子组的简单箱线图:

some_data %>%
        ggplot(aes(x=factor_1,
                   y=some_outcome)) + 
        geom_boxplot(width=0.2,notch = TRUE)

两个箱线图的示例图像

How do you adjust the space between the two boxes, without changing the box width ?如何在不改变盒子宽度的情况下调整两个盒子之间的空间? So I just want to move the boxes closer or further away.所以我只想把盒子移得更近或更远。

No online resourse seems to tackle this specific question.似乎没有在线资源可以解决这个特定问题。 They either show how to change the box width, or they discuss the grouped boxplots that use the fill argument and how to adjust the spacing between the grouped boxes using position_dodge .他们要么展示了如何改变盒子的宽度,要么讨论了使用fill参数的分组箱线图以及如何使用position_dodge调整分组框之间的间距。

Thanks a lot!非常感谢!

You need to remember that you are drawing a plot into a plotting window with a fixed width.您需要记住,您正在将 plot 绘制到具有固定宽度的绘图 window 中。 If you want the boxes to be closer together but remain the same size, and you don't want to change the size of the plotting window, then something needs to take up the extra space you have created.如果您希望框更靠近但保持相同大小,并且您不想更改绘图 window 的大小,则需要占用您创建的额外空间。 This can be achieved either by expanding the x-axis range of the plotting panel itself, or the margins around the plotting panel.这可以通过扩展绘图面板本身的 x 轴范围或绘图面板周围的边距来实现。

Let's start with a concrete example of data so we can run your code:让我们从一个具体的数据示例开始,这样我们就可以运行您的代码:

library(ggplot2)
library(dplyr)

set.seed(1)

some_data <- data.frame(factor_1 = c("0", "1"),
                        some_outcome = rnorm(200))

Now we can use your plotting code to get a reasonable replica of your plot:现在我们可以使用您的绘图代码来获得 plot 的合理副本:

some_data %>%
  ggplot(aes(x=factor_1,
             y=some_outcome)) + 
  geom_boxplot(width = 0.2, notch = TRUE)

在此处输入图像描述

Now, we can move the boxes closer together by expanding the x-axis range like this:现在,我们可以通过像这样扩展 x 轴范围来将框移得更近:

some_data %>%
  ggplot(aes(x=factor_1,
             y=some_outcome)) + 
  geom_boxplot(width = 0.67, notch = TRUE) +
  scale_x_discrete(expand = c(2, 2))

在此处输入图像描述

You can see that the extra space is taken up with some additional room in the panel on either side of the boxes.您可以看到额外的空间被盒子两侧的面板中的一些额外空间占用。

An alternative is to make the panel narrower by increasing the margins around it, thereby allowing whitespace to take up the extra room:另一种方法是通过增加面板周围的边距来使面板更窄,从而允许空白占据额外的空间:

some_data %>%
  ggplot(aes(x=factor_1,
             y=some_outcome)) + 
  geom_boxplot(width = 0.5, notch = TRUE) +
  theme(plot.margin = margin(10, 150, 10, 150))

在此处输入图像描述

I think the second option looks nicer.我认为第二个选项看起来更好。 However, you would get a similar look if you just make your plotting window the correct size, then use your original plotting code (with appropriately selected width parameter) However, you would get a similar look if you just make your plotting window the correct size, then use your original plotting code (with appropriately selected width parameter)

# Drag plotting window to correct size, then run:

some_data %>%
  ggplot(aes(x=factor_1,
             y=some_outcome)) + 
  geom_boxplot(width = 0.4, notch = TRUE)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM