首页 > 解决方案 > 将两条 Kaplan Meier 曲线与 ggsurvplot 合并

问题描述

我有两个不同的数据集,它们基本上包含相同的数据,但一个是针对基线年龄 19 岁或以下 ( data.all.agefs.under19),另一个是针对 19 岁以上 ( data.all.agefs.above19)

每个人的生存对象定义为:

surv.all.agefs.under19 <- Surv(time = data.all.agefs.under19$follow.up.years, event = data.all.agefs.under19$death.specific)
surv.all.agefs.above19 <- Surv(time = data.all.agefs.above19$follow.up.years, event = data.all.agefs.above19$death.specific)

Cox PH 模型定义为:

cox.all.agefs.under19 <- coxph(surv.all.agefs.under19 ~ factor1 + factor2 + factor3, data = data.all.agefs.under19)
cox.all.agefs.above19 <- coxph(surv.all.agefs.above19 ~ factor1 + factor2 + factor3, data = data.all.agefs.above19)

我想为两者创建一个带有 Kaplan Meier 曲线的图,但到目前为止,我只能使用 ggsurvplot 为每个曲线创建一个图:

ggsurv <- ggsurvplot(survfit(cox.all.agefs.under19), data = data.all.agefs.under19, palette = "#2E9FDF", ggtheme = theme_minimal(), legend = "none")
ggsurv <- ggsurvplot(survfit(cox.all.agefs.above19), data = data.all.agefs.above19, palette = "#2E9FDF", ggtheme = theme_minimal(), legend = "none")

那么如何将两条曲线合并到同一个图中呢?

标签: rggplot2survival-analysiscox-regression

解决方案


您可以将数据附加到一个长数据框中,并定义一个变量agegrp来区分两个年龄组。然后你可以绘制如下图。

df1 <- lung
df1$agegrp <- df1$sex

fitme <- survfit(Surv(time, status) ~ agegrp, data = df1) 

  ggsurv2 <- plot(fitme ,  
                  xlim = c(0,1200),
                  main = "Survival curves based on Kaplan-Meier estimates",
                  xlab = "Time in days",      # customize   X axis label.
                  ylab = "Overall survival probability"  # Y axis label
  )
  temp <- lines(fitme, lwd=2:1, col = c("red", "blue"))
  text(temp, c("Under19", "Above19"), adj= -.1) # labels just past the ends
  

输出

更新:

使用 ggsurvplot 如下所示

ggsurv <- ggsurvplot(
    fitme,                   # survfit object with calculated statistics.
    data = df1,              # data used to fit survival curves.
    risk.table = TRUE,       # show risk table.
    pval = TRUE,             # show p-value of log-rank test.
    conf.int = TRUE,         # show confidence intervals for
    # point estimates of survival curves.
    palette = c("#E7B800", "#2E9FDF"),
    xlim = c(0,1200),         # present narrower X axis, but not affect
    # survival estimates.
    xlab = "Time in days",    # customize X axis label.
    break.time.by = 100,      # break X axis in time intervals by 500.
    ggtheme = theme_light(),  # customize plot and risk table with a theme.
    risk.table.y.text.col = T,# colour risk table text annotations.
    risk.table.height = 0.25, # the height of the risk table
    risk.table.y.text = FALSE,# show bars instead of names in text annotations
    # in legend of risk table.
    ncensor.plot = TRUE,      # plot the number of censored subjects at time t
    ncensor.plot.height = 0.25,
    conf.int.style = "step",  # customize style of confidence intervals
    surv.median.line = "hv",  # add the median survival pointer.
    legend.labs = c("Under19", "Above19")  # change legend labels.
  )

输出2:


推荐阅读