如何将参数生存曲线叠加到Kaplan-Meier生存曲线上?
叠加参数生存曲线与Kaplan-Meier曲线的实现方法
已完成的分析代码
Kaplan-Meier分析
KMsufit1_2 <- survfit(survobj1_2 ~ 1, data = set_1_2_grp)
参数生存分析
Gamma1_2 <- flexsurvreg(survobj1_2 ~ 1, data = set_1_2_grp, dist = 'gamma') gen_Gamma1_2 <- flexsurvreg(survobj1_2 ~ 1, data = set_1_2_grp, dist = 'gengamma')
实现方案
以下提供两种常用的实现方式,可根据需求选择:
方式1:基于ggplot2 + survminer的灵活绘图
适合需要自定义图表样式的场景,步骤如下:
- 加载依赖包
library(survminer) library(flexsurv) library(ggplot2)
- 绘制基础Kaplan-Meier曲线并提取ggplot对象
km_plot <- ggsurvplot(KMsufit1_2, data = set_1_2_grp, surv.median.line = "hv", # 可选:添加中位生存时间参考线 ggtheme = theme_bw())$plot
- 提取参数模型的生存曲线数据
# Gamma分布模型的生存数据 gamma_surv_data <- summary(Gamma1_2, type = "survival", tidy = TRUE) # 广义Gamma分布模型的生存数据 gengamma_surv_data <- summary(gen_Gamma1_2, type = "survival", tidy = TRUE)
- 叠加参数曲线并美化图表
km_plot + geom_line(data = gamma_surv_data, aes(x = time, y = est), color = "red", linetype = "dashed") + geom_line(data = gengamma_surv_data, aes(x = time, y = est), color = "blue", linetype = "dotted") + labs(title = "Kaplan-Meier vs 参数生存曲线", x = "时间", y = "生存概率") + scale_color_manual(name = "模型类型", values = c("black", "red", "blue"), labels = c("Kaplan-Meier", "Gamma分布", "广义Gamma分布")) + theme(legend.position = "bottom")
方式2:基于基础绘图系统的快速叠加
代码简洁,适合快速生成对比图:
library(flexsurv) # 绘制Kaplan-Meier曲线 plot(KMsufit1_2, main = "Kaplan-Meier与参数生存曲线对比", xlab = "时间", ylab = "生存概率", col = "black") # 叠加Gamma分布参数曲线 lines(Gamma1_2, type = "survival", col = "red", lty = 2) # 叠加广义Gamma分布参数曲线 lines(gen_Gamma1_2, type = "survival", col = "blue", lty = 3) # 添加图例区分不同曲线 legend("bottomleft", legend = c("Kaplan-Meier", "Gamma分布", "广义Gamma分布"), col = c("black", "red", "blue"), lty = c(1, 2, 3))
内容的提问来源于stack exchange,提问作者Hari
相关产品推荐
相关产品推荐

