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

如何在R的forestplot中任意位置添加文本注释?

解决forestplot添加自定义文本注释的方法

由于forestplot基于grid绘图系统,常规的text()函数无法生效,需要使用grid包的工具来实现程序化添加自定义文本,具体方案如下:

核心思路

利用grid包的grid.text()函数,在forestplot绘制完成后,直接在grid绘图设备中添加文本。支持通过相对坐标、绝对坐标或交互式定位来精准放置注释内容。

具体实现步骤

1. 加载grid包

确保加载grid包(R默认已预装):

library(grid)

2. 添加自定义文本的常见场景

场景1:添加统计量注释到图的角落

提取模型中的效应量、P值等信息,直接添加到图的右上角或其他位置:

# 提取需要展示的统计量
coef_val <- round(age.data$mean[2], 2)
p_val <- round(as.numeric(age.data$`p-val`[2]), 4)
text_content <- paste0("回归系数: ", coef_val, "\nP值: ", p_val)

# 使用相对坐标(npc单位,0-1范围)在右上角添加文本
grid.text(text_content, 
          x = 0.9, y = 0.9, 
          just = "right",  # 文本对齐方式
          gp = gpar(cex = 0.8, fontface = "bold"))  # 设置字号、字体样式

场景2:交互式获取精准坐标

如果不确定目标位置的坐标,可以先用grid.locator()获取点击位置的坐标,再填入grid.text():

# 先绘制森林图
print(age.plot)
# 点击图中想要添加文本的位置,控制台会输出坐标(如 x=0.2, y=0.3)
grid.locator(unit = "npc")
# 用获取到的坐标添加文本
grid.text("自定义注释", 
          x = 0.2, y = 0.3, 
          just = "left", 
          gp = gpar(col = "red", cex = 0.7))

场景3:在特定行附近添加注释

如果要在某一行(比如Group 2的行)旁添加注释,可以结合图的行分布调整y坐标:

# 假设Group 2的行对应y=0.4的位置(根据实际图调整)
grid.text("年龄均值显著降低", 
          x = 0.1, y = 0.4, 
          just = "left", 
          gp = gpar(col = "blue"))

3. 完整整合代码

将添加注释的代码整合到原绘图流程中:

library(forestplot)
library(tidyr)
library(grid)

# 原始数据与模型拟合
cohort <- data.frame(Age = c(43, 39, 34, 55, 70, 59, 44, 83, 76, 44, 
                             75, 60, 62, 50, 44, 40, 41, 42, 37, 35, 55, 46), 
                     Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                         1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1","2"), class = "factor"))

age.lm <- lm(Age ~ Group, data = cohort)

age.data <- summary(age.lm)$coefficients[2,]

age.data <- rbind(c(0,0,0,1,"Group 1", "n=15"),
                  c(age.data[1], age.data[1]-age.data[2]*1.95, 
                  age.data[1]+age.data[2]*1.95, age.data[4], "Group 2", "n=7"))

colnames(age.data) <- c("mean","lower","upper","p-val","labeltext","numbers")

age.data <- data.frame(age.data)

age.data$mean <- as.numeric(age.data$mean)
age.data$lower <- as.numeric(age.data$lower)
age.data$upper <- as.numeric(age.data$upper)

# 绘制森林图
age.plot <- forestplot(age.data,
           labeltext = c(labeltext,numbers),
           boxsize = 0.1,
           xlog = FALSE,
           clip=c(-20,20),
           xticks=c(-20,-10,0,10,20),
           txt_gp = fpTxtGp(ticks=gpar(cex=1)),
           align=c("l","c","l"))

print(age.plot)

# 添加自定义文本注释
coef_val <- round(age.data$mean[2], 2)
p_val <- round(as.numeric(age.data$`p-val`[2]), 4)
grid.text(paste0("Coeff: ", coef_val, "\nP: ", p_val), 
          x = 0.9, y = 0.9, just = "right", 
          gp = gpar(cex = 0.8, fontface = "bold"))

grid.text("两组年龄差异对比", x = 0.1, y = 0.9, just = "left", gp = gpar(col = "darkgreen"))

关键说明

  • grid.text()的x/y参数默认使用npc单位(0代表左/下边缘,1代表右/上边缘),适配不同尺寸的绘图设备。
  • gpar()参数可自定义文本样式:cex控制字号,col控制颜色,fontface设置粗体/斜体等。
  • 多行文本可通过字符串中的\n实现换行,或多次调用grid.text()添加。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 22:40:05