如何将 ggplot2 箱线图分组为异质组(不是通常的箱线图分组)?

How to group ggplot2 boxplots into heterogenous groups (not the usual grouping of boxplots)?

假设我有 5 个箱线图,例如 5 月到 9 月的臭氧水平。

rm(list = ls())
library(datasets)
library(ggplot2)

data(airquality)
airquality$Month <- factor(airquality$Month,
                           labels = c("May", "Jun", "Jul", "Aug", "Sep"))

plot <- ggplot(airquality, aes(x = Month, y = Ozone, fill = Month)) +
  geom_boxplot()
plot

现在我想将它们直观地分组,例如 2+1+2: 所以我想在 6 月和 7 月以及 7 月和 8 月之间有一个间隔。 (实际分组可能不同)。

通常的箱线图分组方法似乎没有解决这个问题。

这可能是作弊,但我不太确定您对 x 轴上的标签有多关心。

library (dplyr) # for case_when()
special_x <- case_when(airquality$Month == "May" ~ 1,
              airquality$Month == "Jun" ~ 2,
              airquality$Month == "Jul" ~ 4,
              airquality$Month == "Aug" ~ 6,
              airquality$Month == "Sep" ~ 7)

    airquality$special_x <- special_x

    ggplot(airquality, aes(x = special_x, y = Ozone, fill = Month)) +
      geom_boxplot()

此外,您可以添加 + labs(x="") + theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())

更新

正如评论所说,

ggplot(airquality, aes(x = special_x, y = Ozone, fill = Month)) +
  geom_boxplot()+
  scale_x_continuous(breaks = c(1,2,4,6,7),
                     labels =c("May", "Jun", "Jul", "Aug", "Sep"))+
  labs(x="")

会产生

您可以创建一个变量来对您的 x 变量进行分组 - 该方法取决于您的数据,但 forcats::fct_collapse 是一种简单的方法。然后用它来刻面图。使用 facet_grid,您可以设置自由 x-scale 和自由间距,以便根据每个面板的箱线图数量调整面板大小。

library(dplyr)
library(ggplot2)
data(airquality)
airquality$Month <- factor(airquality$Month,
                           labels = c("May", "Jun", "Jul", "Aug", "Sep"))

air_groups <- airquality %>%
  mutate(group = forcats::fct_collapse(Month, 
                              "group 1" = c("May", "Jun"),
                              "group 2" = c("Jul"),
                              "group 3" = c("Aug", "Sep")))
ggplot(air_groups, aes(x = Month, y = Ozone, fill = Month)) +
  geom_boxplot() +
  facet_grid(cols = vars(group), scales = "free_x", space = "free")

如果您想掩饰您正在使用分面这一事实,或者这些组本身实际上没有意义,您可以删除分面标签。还有其他主题参数可以在这里进行试验,例如面板之间的间距。

ggplot(air_groups, aes(x = Month, y = Ozone, fill = Month)) +
  geom_boxplot() +
  facet_grid(cols = vars(group), scales = "free_x", space = "free") +
  theme(strip.text = element_blank())
#> Warning: Removed 37 rows containing non-finite values (stat_boxplot).