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

如何消除patchwork拼接两个ggplot2图形间的多余空白?

问题说明

我使用df1和df2创建了上下拼接的ggplot2图形,尝试多种方法仍无法消除两图间红色圈出的多余空白,已参考相关技术帖子但问题未解决。

绘图代码
who1pred <- ggplot(filter(df1), 
                   aes(x = ki67, y = pred, color = time, fill = time)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), color = NA) +
  geom_line() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50))  + 
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank()
        )


who1dist <- ggplot(filter(df2), 
                   aes(x = Ki67)) +
  geom_histogram() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50))
基础拼接代码
who1pred / who1dist +
  plot_layout(ncol = 1, heights = c(1, .4))  
已尝试的解决方案
# 尝试添加plot_spacer
who1pred / plot_spacer() / who1dist +
  plot_layout(ncol = 1, heights = c(1, 0, .4),
              guides = "collect")

# 尝试修改plot.margin
who1pred / who1dist +
  plot_layout(ncol = 1, heights = c(1, .4) & theme(plot.margin = c(0, 0, 0, 0)))
数据
df1 <- structure(list(WHO = c("2", "2", "3", "1", "1", "2", "2", "3", 
                              "1", "3"), ki67 = c(94, 64, 51, 15, 18, 74, 93, 45, 65, 62), 
                      pred = c(0.464410116302291, 0.897401711241776, 0.700242375617693, 
                               0.358678057582871, 0.390573594975723, 0.829113422265248, 
                               0.48802465997491, 0.666022132399912, 0.540540217190029, 0.699322187068211
                      ), time = c("120", "120", "120", "120", "120", "120", "120", 
                                  "120", "120", "60"), lower = c(0.0137577850103707, 0.785671003373972, 
                                                                 0.540479287582779, 0.282505841174618, 0.282542223489564, 
                                                                 0.654223374533307, 0.0466809675600842, 0.490877830284619, 
                                                                 0, 0.550598433238934), upper = c(0.915062447594212, 1, 0.860005463652608, 
                                                                                                  0.434850273991124, 0.498604966461882, 1, 0.929368352389736, 
                                                                                                  0.841166434515205, 1, 0.848045940897488)), row.names = c(NA, 
                                                                                                                                                           -10L), class = c("tbl_df", "tbl", "data.frame"))
df2 <- structure(list(Ki67 = c(1, 2, 5, 11, 1, 1, 8, 1, 4, 2), WHO = c("1", 
                                                                "1", "2", "2", "1", "1", "2", "1", "1", "2")), row.names = c(NA, 
                                                                                                                             -10L), class = "data.frame")
可行解决方案

解决空白问题的核心是分别调整两个图的plot.margin,抵消上下边距的空白,同时彻底移除上方图形x轴的占位空间,具体修改如下:

  1. 调整上方图形who1pred:
    • 新增axis.title.x = element_blank(),彻底清除x轴标题的占位空白
    • 设置底部边距为负值,拉近与下方图形的距离
  2. 调整下方图形who1dist:
    • 设置顶部边距为负值,抵消上方图形的底部空白

修改后的绘图代码:

who1pred <- ggplot(filter(df1), 
                   aes(x = ki67, y = pred, color = time, fill = time)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), color = NA) +
  geom_line() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50))  + 
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(), # 彻底移除x轴标题占位
        plot.margin = unit(c(5.5, 5.5, -10, 5.5), "pt") # 底部边距设为负
        )


who1dist <- ggplot(filter(df2), 
                   aes(x = Ki67)) +
  geom_histogram() +
  scale_x_continuous(name = "",
                     breaks = seq(0, 50, 10)) +
  coord_cartesian(xlim = c(0, 50)) +
  theme(plot.margin = unit(c(-10, 5.5, 5.5, 5.5), "pt")) # 顶部边距设为负

然后用原拼接代码即可:

who1pred / who1dist +
  plot_layout(ncol = 1, heights = c(1, .4))  

你可以根据实际显示效果微调-10这个数值,直到空白完全消除。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 21:30:54