更改基于 x 变量的箱线图背景 (ggplot2)

Change the boxplot background based in x-variable (ggplot2)

我想更改基于 x 变量的箱线图的背景。我的代码很简单:

ggplot(data = df, aes(x = variable, y = value)) + 
  geom_boxplot() + 

所以,我有 17 个 x 变量,并且我在同一张图片中生成了 17 个箱线图。我想将箱线图的背景从 1 变为灰色,从 11 变为 14。我不知道该怎么做。

谢谢。

你必须创造一些因素来帮助这个过程。在示例中,我在 df.

中创建了一个新功能 (tales)
library(tidyverse)
df <- data.frame(variable = rep(base::LETTERS[1:17], 5),
                 value = runif(17*5, 0, 100))
df <- df %>% 
  dplyr::mutate(tales = rep(c(rep("x", 4), rep("y", 11-4), rep("w", 17-11)), 5))

ggplot(data = df, aes(x = variable, y = value)) + 
  geom_boxplot(aes(fill = tales))