You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot2中为两组箱线图添加间距?

问题

现有一份数据集,已绘制箱线图,希望在两组数据之间添加间距。当前用虚线分隔两组,尝试过facet_wrap但生成了双Y轴,不符合需求。

数据集

structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L), levels = c("dm4m_c", "dm4l_c", "dm3_c", 
"dm2_c", "dm1_c", "dldm_c", "dm4m_i", "dm4l_i", "dm3_i", "dm2_i", 
"dm1_i"), class = "factor"), value = c(4.54, 3.73, 3.06, 2.92, 
3.01, 4.45, 2.36, 3.35, 4.35, 5.67, 2.91, 3.72, 5.39, 3.4, 3.07, 
1.2, 0.79, 0.43, 0.17, 0.03, 0.6, 0.15, -0.24, 0.98, 0.15, -0.37, 
0.2, 1.71, 0.46, 0.26, 0.46, 0.32, 0.49, -0.03, 0.39, 0.6, 0.47, 
0.32, 1.17, 0.55, -0.48, 0.13, 1.65, -0.2, 0.05, 2.86, 2.19, 
2.06, 1.66, 2, 2.95, 0.45, 1.29, 1.96, 1.07, 0.52, 0.27, 3.17, 
1.24, 1.03, -0.24, -0.41, -0.45, NA, NA, NA, -0.36, -0.39, -0.13, 
-0.49, -0.81, -0.58, 0.69, -0.93, -0.43, 0.28, -0.14, -0.04, 
-0.31, 0.27, 0.34, 0.9, 0.28, 0.8, 0.25, -0.37, -0.35, 0.59, 
-0.29, -0.07, 3.8, 2.28, 2.43, 1.29, 1.01, 2.65, 1.75, 2.42, 
3.86, 4.74, 1.7, 1.74, 2.55, 1.58, 1.32, 0.88, 0.31, 0.2, -0.9, 
-1.14, 0.4, 0.06, 0.06, 0.69, 0.81, 0.24, 0.17, 0.73, -0.1, 0.35, 
0.28, 0.03, -0.01, 0.13, 0.42, 0.79, 0.18, -0.02, 0.43, 1.68, 
-0.08, 0.23, 0.98, -0.6, -0.23, 2.02, 1.24, 1.46, 0.8, 0.89, 
1.89, 0.19, 0.55, 1.34, 1.05, 0, 0.04, 1.81, 0.2, 0.21, -0.09, 
-0.41, -0.4, NA, NA, NA, -0.54, -0.77, -0.32, -0.04, -0.98, -0.29, 
0.8, -0.99, -0.68), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1", 
"2"), class = "factor")), row.names = c(NA, -165L), class = "data.frame")

当前代码

ggplot(data_long2, aes(x = variable, y = value,fill=variable)) +            # Applying ggplot function
  geom_boxplot(position=position_dodge(width=5)) +
  geom_point() +
  stat_summary(fun.y="mean", color="white", shape=7)+
  theme_classic()+
  ggtitle("Slop") +
  xlab("Zones") + ylab("Slop") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),legend.position = "none") +
  geom_vline(xintercept = 6.5, linetype = "dashed")
解决方案

方法一:修正分面设置(推荐)

你之前用facet_wrap出现双Y轴,应该是误设置了scales="free"。只需指定scales="free_x",就能让X轴独立展示两组变量,同时共享同一Y轴,再通过panel.spacing.x调整组间间距即可:

ggplot(data_long2, aes(x = variable, y = value, fill = variable)) +
  geom_boxplot() +
  geom_point() +
  stat_summary(fun = mean, color = "white", shape = 7) +
  theme_classic() +
  ggtitle("坡度") +
  xlab("区域") + ylab("坡度") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5), legend.position = "none") +
  # 按group分面,仅X轴自由,Y轴共享
  facet_wrap(~group, scales = "free_x", nrow = 1) +
  # 调整两组之间的空隙大小,可根据需求修改数值
  theme(panel.spacing.x = unit(1.5, "cm"))

方法二:手动偏移X轴位置

如果不想用分面,可以给不同组的变量分配偏移后的X轴位置,直接在同一坐标系中留出空隙:

# 预处理数据,给组2的变量X位置加偏移量
library(dplyr)
data_long2 <- data_long2 %>%
  mutate(
    x_pos = as.integer(variable) + ifelse(group == "2", 2, 0)
  )

# 绘制箱线图
ggplot(data_long2, aes(x = x_pos, y = value, fill = variable)) +
  geom_boxplot() +
  geom_point() +
  stat_summary(fun = mean, color = "white", shape = 7) +
  theme_classic() +
  ggtitle("坡度") +
  xlab("区域") + ylab("坡度") +
  # 设置X轴标签,匹配偏移后的位置
  scale_x_continuous(
    breaks = c(1:6, 8:12),
    labels = levels(data_long2$variable)
  ) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5), legend.position = "none") +
  # 保留虚线分隔两组
  geom_vline(xintercept = 7, linetype = "dashed")

内容的提问来源于stack exchange,提问作者Pooyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 12:27:02