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

如何为patchwork合并的两个ggplot2图添加统一X轴标题?

为patchwork合并的ggplot2图添加统一X轴标题

我使用patchwork包合并了两个独立的ggplot2绘图,现需为合并后的图添加一个统一的X轴标题(例如"SD单位差异"),相关示例代码如下:

dat_F <- structure(list(
  term = c("ExpA", "ExpA", "ExpB", "ExpB"), 
  estimate = c(-3.802316239, -5.885048428, -3.678601513, -4.103813546), 
  lci = c(-4.42722285, -6.476932582, -4.332540471, -4.751382827), 
  uci = c(-3.177409628, -5.293164273, -3.024662556, -3.456244265), 
  out = c("Out1", "Out2", "Out1", "Out2")), 
  class = "data.frame", row.names = c(NA, -4L))

dat_M <- structure(list(
  term = c("ExpA", "ExpA", "ExpB", "ExpB"), 
  estimate = c(-1.134138758, -2.232236452, -0.935606149, -1.497766819), 
  lci = c(-1.841890314, -2.894980123, -1.662179615, -2.180915403), 
  uci = c(-0.426387201, -1.569492781, -0.209032682, -0.814618236), 
  out = c("Out3", "Out4", "Out3", "Out4")), 
  class = "data.frame", row.names = c(NA, -4L))

library(tidyverse)
library(patchwork)
library(RColorBrewer)

(plot_F <- ggplot(data = dat_F, aes(
  x = term, y = estimate, ymin = lci, ymax = uci)) + 
    theme_classic() + geom_pointrange(size = 0.3, aes(col = term)) + coord_flip() +
    ggtitle("a. Females") + geom_hline(yintercept = 0, lty = 1, size = 0.1, col = "red") + 
    facet_wrap(~ out, ncol = 1) + scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(-7, 0.5), breaks=c(-6, -4, -2, 0)) + 
    ylab("")  + theme(axis.title.y = element_blank(), axis.ticks.y = element_blank(), 
      strip.background = element_blank(), axis.text.y = element_blank(), 
      legend.title = element_blank(), legend.position  = "bottom") +
    guides(colour = guide_legend(override.aes = list(size=0.8)))) 

(plot_M <- ggplot(data = dat_M, aes(
  x = term, y = estimate, ymin = lci, ymax = uci)) + 
    theme_classic() + geom_pointrange(size = 0.3, aes(col = term)) + coord_flip() +
    ggtitle("b. Males") + geom_hline(yintercept = 0, lty = 1, size = 0.1, col = "red") + 
    facet_wrap(~ out, ncol = 1) + scale_color_brewer(palette = "Set2") +
    scale_y_continuous(limits = c(-7, 0.5), breaks=c(-6, -4, -2, 0)) + 
    ylab("")  + theme(axis.title.y = element_blank(), axis.ticks.y = element_blank(), 
                      strip.background = element_blank(), axis.text.y = element_blank(), 
                      legend.title = element_blank(), legend.position  = "bottom") +
    guides(colour = guide_legend(override.aes = list(size=0.8)))) 

(combined_plot <- (plot_F + plot_M) + 
  plot_layout(guides = "collect") & theme(
    legend.position = 'bottom', 
    legend.direction = "horizontal",
    text = element_text(size = 8)))

合并后的绘图效果:左右两个面板分别展示女性和男性的分析结果,每个面板包含两个分面,呈现不同结局的点估计与置信区间,目前无统一的X轴标题。


解决方案

由于绘图使用了coord_flip(),原ggplot的Y轴被转为显示在底部的X轴,因此可以通过以下两种方式添加统一标题:

方法1:全局主题统一设置

直接在合并图的全局主题中设置axis.title.x,这是最简洁的方式:

(combined_plot <- (plot_F + plot_M) + 
  plot_layout(guides = "collect") & 
  theme(
    legend.position = 'bottom', 
    legend.direction = "horizontal",
    text = element_text(size = 8),
    axis.title.x = element_text("SD单位差异", size = 8)  # 添加统一X轴标题
  ))

方法2:自定义位置标注(更灵活)

如果需要调整标题的位置,可以使用patchwork的inset_element()配合grid包的文本对象:

library(grid)

combined_plot <- (plot_F + plot_M) + 
  plot_layout(guides = "collect") & 
  theme(
    legend.position = 'bottom', 
    legend.direction = "horizontal",
    text = element_text(size = 8)
  )

# 添加自定义位置的统一标题
combined_plot + 
  inset_element(
    textGrob("SD单位差异", gp = gpar(fontsize = 8)),
    left = 0.2, right = 0.8, bottom = 0, top = 0.05  # 调整位置参数
  )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 22:00:23