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

如何用R的survminer与splots为Kaplan Meier曲线添加A、B标注

你可以直接对ggsurvplot返回结果中的ggplot绘图对象添加文本标注层即可实现需求,修改后完整代码如下:

library(survival)
library(survminer)
library(ggplot2)

# 拟合生存曲线
fit<- survfit(Surv(time, status) ~ sex, data = lung)

# 生成生存图列表
splots <- list()
# 第一张图,添加标注A
splots[[1]] <- ggsurvplot(fit, data = lung, risk.table = TRUE, ggtheme = theme_minimal())
splots[[1]]$plot <- splots[[1]]$plot + 
  annotate("text", x = -Inf, y = Inf, label = "A", 
           size = 12, fontface = "bold", hjust = -0.3, vjust = 1.2)
# 第二张图,添加标注B
splots[[2]] <- ggsurvplot(fit, data = lung, risk.table = TRUE, ggtheme = theme_grey())
splots[[2]]$plot <- splots[[2]]$plot + 
  annotate("text", x = -Inf, y = Inf, label = "B", 
           size = 12, fontface = "bold", hjust = -0.3, vjust = 1.2)

# 拼接并打印绘图
arrange_ggsurvplots(splots, print = TRUE,
  ncol = 2, nrow = 1, risk.table.height = 0.4)

# 保存为PDF
res <- arrange_ggsurvplots(splots, print = FALSE)
ggsave("myfile.pdf", res, width = 15, height = 15, units = "cm")

你可以根据自己的需求调整annotate中的参数:

  • size:控制标注字母的字号
  • fontface:设置为bold就是粗体,不需要可以删掉这个参数
  • hjust/vjust:调整标注和左上角的距离,数值越大距离越远

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 20:06:00