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

使用ggarrange()排版图表:调整y轴标签大小、添加标题的自定义方法

问题场景

使用ggarrange()函数组合4张带有公共图例的ggplot2图表,因涉及多组不同数据集,选择ggarrange()而非其他图表组合函数。
当前存在两个待解决问题:

  • 组合后图表的y轴标签出现重叠
  • 未找到为组合图表添加总标题、完成其他自定义配置的方法

当前实现代码如下:

plot_velocity_uni <- ggplot() +
  xlim(c(0, 200)) +
  stat_function(fun = fun.1, aes(color = "Left atrium")) +
  geom_function(fun = fun.2, aes(color = "Left ventricle")) +
  geom_function(fun = fun.3, aes(color = "Right atrium")) +
  geom_function(fun = fun.4, aes(color = "Right ventricle")) +
  xlab("heart rate")+ # x轴标签
  ylab("estimated unipolar conduction velocity (m/s)")+ # y轴标签
  theme(legend.position = "none", aspect.ratio = 1)

plot_velocity_bi <- ggplot() +
  xlim(c(0, 200)) +
  stat_function(fun = fun.1, aes(color = "Left atrium")) +
  geom_function(fun = fun.2, aes(color = "Left ventricle")) +
  geom_function(fun = fun.3, aes(color = "Right atrium")) +
  geom_function(fun = fun.4, aes(color = "Right ventricle"))+
  xlab("heart rate")+ # x轴标签
  ylab("estimated bipolar conduction velocity (m/s)")+ # y轴标签
  theme(legend.position = "none", aspect.ratio = 1)

plot_voltage_uni <- ggplot() +
  xlim(c(0, 200)) +
  stat_function(fun = fun.1, aes(color = "Left atrium")) +
  geom_function(fun = fun.2, aes(color = "Left ventricle")) +
  geom_function(fun = fun.3, aes(color = "Right atrium")) +
  geom_function(fun = fun.4, aes(color = "Right ventricle")) +
  xlab("heart rate")+ # x轴标签
  ylab("estimated unipolar conduction velocity (m/s)")+ # y轴标签
  theme(legend.position = "none", aspect.ratio = 1)

plot_voltage_bi <- ggplot() +
  xlim(c(0, 200)) +
  stat_function(fun = fun.1.bi, aes(color = "Left atrium")) +
  geom_function(fun = fun.2.bi, aes(color = "Left ventricle")) +
  geom_function(fun = fun.3.bi, aes(color = "Right atrium")) +
  geom_function(fun = fun.4.bi, aes(color = "Right ventricle")) +
  xlab("heart rate")+ # x轴标签
  ylab("estimated unipolar conduction velocity (m/s)")+ # y轴标签
  theme(legend.position = "none", aspect.ratio = 1)

ggarrange(plot_velocity_uni, plot_velocity_bi, plot_voltage_uni, plot_voltage_bi, ncol=2, nrow=2, common.legend = TRUE, legend="right", labels = c("A", "B", "C", "D"))

当前绘图效果示例:
当前绘图效果示例

解决方法

1. 修复y轴标签重叠问题

标签重叠核心原因是子图间距不足、子图绘图区域未对齐,通过两个设置即可解决:

  • 给每个子图增加统一的边距预留标签空间:在每个子图的theme()中添加plot.margin = margin(t = 8, r = 12, b = 8, l = 12, unit = "pt"),上下左右边距数值可根据标签长度调整。注意现有代码中plot_voltage_bi的y轴标签文本错误,和单极传导速度的标签重复,需要修正为对应双极电压的标签文本。
  • 在ggarrange()中添加align = "hv"参数,强制所有子图的水平、垂直坐标轴完全对齐,避免因绘图区域大小错位导致的标签挤压。

2. 添加组合图总标题及全局自定义配置

ggarrange()本身不支持直接添加全局标题,需要搭配ggpubr包内置的annotate_figure()函数实现,该函数支持给组合图添加总标题、副标题、脚注,还可调整全局边距等配置。

修改后的完整组合代码如下:

library(ggpubr) # 确保已加载ggpubr包

# 先给每个子图统一加边距,修正错误的y轴标签
plot_velocity_uni <- plot_velocity_uni + theme(plot.margin = margin(8,12,8,12))
plot_velocity_bi <- plot_velocity_bi + theme(plot.margin = margin(8,12,8,12))
plot_voltage_uni <- plot_voltage_uni + theme(plot.margin = margin(8,12,8,12))
plot_voltage_bi <- plot_voltage_bi + 
  ylab("estimated bipolar voltage (mV)") + # 修正复制粘贴导致的错误y轴标签
  theme(plot.margin = margin(8,12,8,12))

# 生成组合图,添加align参数对齐坐标轴
combined_plot <- ggarrange(plot_velocity_uni, plot_velocity_bi, 
                           plot_voltage_uni, plot_voltage_bi, 
                           ncol=2, nrow=2, 
                           common.legend = TRUE, 
                           legend="right", 
                           labels = c("A", "B", "C", "D"),
                           align = "hv") # 对齐坐标轴避免错位

# 添加总标题等全局配置
final_plot <- annotate_figure(combined_plot,
                              top = text_grob("心脏电生理参数随心率变化趋势", 
                                              size = 14, face = "bold"),
                              bottom = text_grob("注:数据为模型估算结果", size = 10))
# 输出图片
print(final_plot)

如果需要更精细的全局样式调整,还可以在annotate_figure()中通过fig.lab、left、right参数添加全局的坐标轴标签、图侧标注等元素。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 04:54:17