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

如何为ggplot手动绘制的森林图左侧数据表添加变量标题?

手动绘制森林图添加左侧数据表标题的实现方法

以下两种方法都能在你用ggplot2+gridExtra搭建的森林图左侧数据表区域添加COHORT、n、mean_age标题,附可复现代码:

方法1:单独制作标题面板后拼接

这种方式把标题和数据内容分成两个独立的ggplot对象,再垂直拼接成左侧面板,灵活性更高,方便单独调整标题样式。

library(ggplot2)
library(gridExtra)

# 示例数据集(替换成你的真实数据)
df <- data.frame(
  COHORT = c("Study 1", "Study 2", "Study 3", "Study 4"),
  n = c(120, 89, 210, 156),
  mean_age = c(54.2, 61.5, 48.9, 57.3),
  OR = c(1.23, 0.89, 1.56, 1.11),
  lower = c(0.98, 0.72, 1.21, 0.90),
  upper = c(1.55, 1.10, 1.99, 1.36)
)

# 1. 制作标题行面板
title_panel <- ggplot() +
  geom_text(aes(x = 1, y = 1, label = "COHORT"), hjust = 0, size = 4, fontface = "bold") +
  geom_text(aes(x = 2, y = 1, label = "n"), hjust = 0.5, size = 4, fontface = "bold") +
  geom_text(aes(x = 3, y = 1, label = "mean_age"), hjust = 0.5, size = 4, fontface = "bold") +
  theme_void() +
  xlim(0.5, 3.5) +
  theme(plot.margin = margin(5, 5, 0, 5)) # 调整边距避免和下方数据重叠

# 2. 制作原数据表面板
data_panel <- ggplot(df, aes(x = 1, y = reorder(COHORT, -OR))) +
  geom_text(aes(label = COHORT), hjust = 0, size = 3.5) +
  geom_text(aes(x = 2, label = n), hjust = 0.5, size = 3.5) +
  geom_text(aes(x = 3, label = mean_age), hjust = 0.5, size = 3.5) +
  theme_void() +
  xlim(0.5, 3.5) +
  theme(plot.margin = margin(0, 5, 5, 5))

# 3. 拼接标题和数据面板成左侧整体
left_panel <- grid.arrange(title_panel, data_panel, ncol = 1, heights = c(0.1, 0.9))

# 4. 制作森林图主体(替换成你已有的森林图代码)
forest_panel <- ggplot(df, aes(x = OR, y = reorder(COHORT, -OR))) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray50") +
  geom_point(size = 3) +
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 0.2) +
  scale_x_log10(limits = c(0.7, 2)) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

# 5. 组合左侧面板和森林图
grid.arrange(left_panel, forest_panel, ncol = 2, widths = c(0.4, 0.6))

方法2:在数据集顶部插入标题行

直接在你的数据框顶部添加一行标题内容,绘制时通过样式区分标题和数据,步骤更简洁。

library(ggplot2)
library(gridExtra)

# 示例数据集(替换成你的真实数据)
df <- data.frame(
  COHORT = c("Study 1", "Study 2", "Study 3", "Study 4"),
  n = c(120, 89, 210, 156),
  mean_age = c(54.2, 61.5, 48.9, 57.3),
  OR = c(1.23, 0.89, 1.56, 1.11),
  lower = c(0.98, 0.72, 1.21, 0.90),
  upper = c(1.55, 1.10, 1.99, 1.36)
)

# 1. 在数据集顶部插入标题行
df_with_title <- rbind(
  data.frame(COHORT = "COHORT", n = "n", mean_age = "mean_age", OR = NA, lower = NA, upper = NA),
  df
)

# 2. 绘制带标题的数据表面板
left_panel <- ggplot(df_with_title, aes(x = 1, y = reorder(COHORT, -OR))) +
  geom_text(aes(label = COHORT), hjust = 0, 
            size = ifelse(row_number() == 1, 4, 3.5),
            fontface = ifelse(row_number() == 1, "bold", "plain")) +
  geom_text(aes(x = 2, label = n), hjust = 0.5, 
            size = ifelse(row_number() == 1, 4, 3.5),
            fontface = ifelse(row_number() == 1, "bold", "plain")) +
  geom_text(aes(x = 3, label = mean_age), hjust = 0.5, 
            size = ifelse(row_number() == 1, 4, 3.5),
            fontface = ifelse(row_number() == 1, "bold", "plain")) +
  theme_void() +
  xlim(0.5, 3.5) +
  theme(plot.margin = margin(5, 5, 5, 5))

# 3. 制作森林图主体(替换成你已有的森林图代码)
forest_panel <- ggplot(df, aes(x = OR, y = reorder(COHORT, -OR))) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray50") +
  geom_point(size = 3) +
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 0.2) +
  scale_x_log10(limits = c(0.7, 2)) +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

# 4. 组合左侧面板和森林图
grid.arrange(left_panel, forest_panel, ncol = 2, widths = c(0.4, 0.6))

你可以根据自己的需求调整标题的字体大小、对齐方式、边距等样式,适配你的森林图整体风格。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 15:25:30