如何手动增加 R 中分组箱形图中两个特定箱子之间的间距?

How to manually increase spacing between two specific boxes within a grouped box plot in R?

有没有办法增加此箱形图中黄色和红色框之间的间距?

set.seed(40)
df <- data.frame(
  Outcome = runif(60), 
  Fruit = rep(1:3, each = 10), 
  Freshness = rep(c(0, 0.5), each = 30), 
  Farm = factor(rep(c("A", "B"), each = 5))
) %>% 
  transform(
  Outcome = Outcome*Fruit+Freshness, 
  Fruit = as.factor(Fruit), 
  Freshness = as.factor(Freshness)
)

ggplot(data = df, aes(Farm, Outcome, col = Freshness, fill = Fruit)) + 
  geom_boxplot() + 
  scale_color_manual(values = c("lightslategrey", "black"), labels = c("Stale", "Fresh")) + 
  scale_fill_manual(values = c("red", "orange", "yellow"), labels = c("Apples", "Oranges", "Bananas"))

我想增加每个 "Farm" 组中 "Freshness" 颜色组之间的间距(或留出间隙),但不要太大以至于框与 "Farm"组。也就是说,我只想增加黄色和红色框之间的间距以强调"Freshness"组之间的差异。

"Fruit" 组仍将保持组内框之间的距离。也就是说,相邻的红色、橙色和黄色框将保持靠近。

您可以通过将 position=position_dodge(width =...)) 添加到 geom_boxplot() 并调整宽度选择来修改框之间的间距,直到满意为止。

ggplot(data = df, aes(Farm, Outcome, col = Freshness, fill = Fruit)) + 
  geom_boxplot(position=position_dodge(width = 1)) + 
  scale_color_manual(values = c("lightslategrey", "black"), labels = c("Stale", "Fresh")) + 
  scale_fill_manual(values = c("red", "orange", "yellow"), labels = c("Apples", "Oranges", "Bananas"))

这里是原文,供对比。 和修改后的(width=1 增加 X 轴上类别之间的间距是一个不同的问题,而且更难解决。一种简单的解决方法是在 X 轴上使用带有自由刻度的刻面。

ggplot(data = df, aes(Farm, Outcome, col = Freshness, fill = Fruit)) + 
    geom_boxplot(position=position_dodge(width = 1)) + 
    scale_color_manual(values = c("lightslategrey", "black"), labels = c("Stale", "Fresh")) + 
    scale_fill_manual(values = c("red", "orange", "yellow"), labels = c("Apples", "Oranges", "Bananas")) +
    facet_wrap(~Farm, ncol = 2, scales = "free_x")

除了 dshkol 的回答之外,您还可以增加组之间的间距,但这有点像 hack。首先,您需要创建介于农场 A 和农场 B(即 AA)之间的虚拟数据并绘制它。因为图中没有数据,所以图表上没有任何内容。接下来,您需要将中断指定为 A 和 B,这样虚拟 AA 就不会显示在绘图上。您可以使用 position_dodge 参数调整箱线图之间的间距,并通过调整 expand 参数更改组之间的间距。箱线图的显示距离也取决于您导出的图像尺寸。

# get unique values to set scale_x_discrete labels
farms <- unique(df$Farm)
dfmod <- df

# create dummy row with no data except for Farms
dfmod$Farm <- as.character(dfmod$Farm)
dfmod <- rbind(dfmod, list(NA, NA, NA, 'AA'))


ggplot(data = dfmod, aes(Farm, Outcome, col = Freshness, fill = Fruit)) + 
  geom_boxplot(position=position_dodge(1)) + 
  scale_color_manual(values = c("lightslategrey", "black"), labels = c("Stale", "Fresh")) + 
  scale_fill_manual(values = c("red", "orange", "yellow"), labels = c("Apples", "Oranges", "Bananas")) +
  scale_x_discrete(breaks=farms, expand=c(0,2))

这里有技巧:

完成了以下事情:

  • 使用超出范围的值(在本例中为负值)创建新的 Fruit
  • 将 ylim 限制为正值,以防止在绘图中显示这种新水果
  • 捏造图例不显示该水果(space 仍然保持不变)
  • 隐藏图例背景使图例中的空白space不显示
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
set.seed(40)
df <- data.frame(
  Outcome = runif(60), 
  Fruit = rep(1:3, each = 10), 
  Freshness = rep(c(0, 0.5), each = 30), 
  Farm = factor(rep(c("A", "B"), each = 5))
) %>% 
  transform(
    Outcome = Outcome*Fruit+Freshness, 
    Fruit = as.factor(Fruit), 
    Freshness = as.factor(Freshness)
  )

dfe <- data.frame(
    Outcome = rep(-1,2), 
    Fruit = rep(" ", 2), 
    Freshness = c(0, 0),
    Farm=c("A", "B")
  )

df1 <- rbind(df, dfe) %>% 
  mutate(Fruit = factor(Fruit, levels=unique(Fruit)), 
         Freshness = factor(Freshness, levels=unique(Freshness)))

ggplot(data = df1, aes(Farm, Outcome, col = Freshness, fill = Fruit)) + 
  geom_boxplot(position=position_dodge2(padding=.1)) + 
  scale_color_manual(values = c("lightslategrey", "black"), labels = c("Stale", "Fresh")) + 
  scale_fill_manual(values = c("red", "orange", "yellow", "white"), labels = c("Apples", "Oranges", "Bananas", ""))+
  scale_x_discrete(drop=FALSE) + coord_cartesian(ylim=c(0,max(df1$Outcome)))+
  guides(fill = guide_legend(override.aes = list(size = c(rep(.6, 3), 0), fill = c("red", "orange", "yellow", NA))))+
  theme(legend.key = element_rect(fill = "white"))

reprex package (v0.3.0)

于 2020-05-26 创建