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

如何移除Alluvial图与堆叠柱状图间空白并实现上下紧密排版?

移除Alluvial图与堆叠柱状图组合后的间隙

我使用ggalluvial、ggplot2、ggpubr等R包绘制了Alluvial图与堆叠柱状图,通过ggarrange上下组合后,图之间存在多余空白,需要将堆叠柱状图紧密置于Alluvial图下方,效果参考目标样式。

原代码

library(ggalluvial)
library(patchwork)
library(cowplot)
library(ggplot2)
library(ggpubr)

# 自定义配色和透明度
color_palette <- c("#003f5c", "#7a5195", "#ef5675", "#ffa600")
alpha_value <- 0.6  # 按需调整透明度

# Alluvial图
alluvial_plot <- ggplot(data = vaccinations,
                        aes(axis1 = survey, axis2 = response, y = freq)) +
  geom_alluvium(aes(fill = response, alpha = alpha_value)) +
  geom_stratum() +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Survey", "Response"),
                   expand = c(0.15, 0.05)) +
  scale_fill_manual(values = color_palette) +
  theme_void() +
  theme(legend.position = "none")  # 移除图例

# 堆叠柱状图
stacked_plot <- ggplot(data = vaccinations, aes(fill = response, y = freq, x = survey)) + 
  geom_bar(position = "fill", stat = "identity", alpha = alpha_value, width = 1, color = "white") + 
  scale_fill_manual(values = color_palette) +
  theme_void()

# 组合图表
combined_plot <- ggarrange(alluvial_plot, stacked_plot,
                    labels = c("A", "B"),
                    ncol = 1, nrow = 2)

效果说明

  • 当前输出:Alluvial图与堆叠柱状图上下排列,二者之间存在明显空白间隔
  • 目标效果:堆叠柱状图完全紧贴Alluvial图底部,无多余空白

解决方案

核心是移除两个图自身的内边距,并在组合时关闭默认间距,以下是修改后的完整代码:

修改后代码(ggarrange版本)

library(ggalluvial)
library(patchwork)
library(cowplot)
library(ggplot2)
library(ggpubr)

color_palette <- c("#003f5c", "#7a5195", "#ef5675", "#ffa600")
alpha_value <- 0.6  

# 调整Alluvial图的底部边距,消除下方空白
alluvial_plot <- ggplot(data = vaccinations,
                        aes(axis1 = survey, axis2 = response, y = freq)) +
  geom_alluvium(aes(fill = response, alpha = alpha_value)) +
  geom_stratum() +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Survey", "Response"),
                   expand = c(0.15, 0.05)) +
  scale_fill_manual(values = color_palette) +
  theme_void() +
  theme(legend.position = "none",
        # 负的底部边距确保完全贴合下方图表
        plot.margin = margin(t = 0, r = 0, b = -5, l = 0))  

# 调整堆叠柱状图的顶部边距,移除上方空白
stacked_plot <- ggplot(data = vaccinations, aes(fill = response, y = freq, x = survey)) + 
  geom_bar(position = "fill", stat = "identity", alpha = alpha_value, width = 1, color = "white") + 
  scale_fill_manual(values = color_palette) +
  theme_void() +
  theme(plot.margin = margin(t = 0, r = 0, b = 0, l = 0))  

# 组合时垂直对齐,关闭间距,设置高度比例
combined_plot <- ggarrange(alluvial_plot, stacked_plot,
                    labels = c("A", "B"),
                    ncol = 1, nrow = 2,
                    align = "v",  # 垂直对齐保证左右边缘一致
                    heights = c(4, 1),  # 按需调整上下图高度占比
                    padding = 0)  # 移除组合时的默认间隙

关键修改点

  1. 调整图的内边距:给Alluvial图设置负的底部边距,堆叠图设置顶部边距为0,消除各自的空白区域
  2. 组合参数优化:align = "v"确保两个图垂直对齐,padding = 0关闭ggarrange的默认间距,heights参数协调上下图的高度比例

备选方案(patchwork版本,更简洁)

如果已经加载patchwork,可用更直观的语法实现:

# 先调整两个图的边距
alluvial_plot <- alluvial_plot + theme(plot.margin = margin(t = 0, r = 0, b = -5, l = 0))
stacked_plot <- stacked_plot + theme(plot.margin = margin(t = 0, r = 0, b = 0, l = 0))

# 直接用/实现上下组合,设置高度比例和标注
combined_plot <- (alluvial_plot / stacked_plot) + 
  plot_annotation(labels = c("A", "B")) +
  plot_layout(heights = c(4, 1))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 17:31:00