1

我一直在尝试将“整体”生存曲线添加到为给定协变量生成的生存曲线中。该曲线是使用 R 中的生存和 ggsurv (GGally) 生成的。

我使用 R 中生存包中的冒号数据集重新创建场景。

    # Load the dataset
    library (survival)
    library(GGally)
    data(colon)

    # This generates the overall survival curve (without covariates):
    kms_avg <- survfit(Surv(time, status)~1, data =colon)
    g_avg <- ggsurv(kms_avg, surv.col="red", xlab="Time (days)", lty.ci=0)
    g_avg



    # This generates the survival curve for covariate 'rx'
    table(colon$rx)
    kms_rx <- survfit(Surv(time, status)~rx, data =colon)
    g_rx <- ggsurv(kms_rx, surv.col="red", xlab="Time (days)", lty.ci=0)
    g_rx

现在我需要将总体生存线添加到图 g_rx 中。g_rx, g_avg 是 ggplot2 对象。

# Extract the time, surv values (x,y axis values) from g_avg object  
s_avg <- summary(kms_avg) 
s_time <- s_avg$time 
s_surv <- s_avg$surv 
s <- data.frame(time=s_time, surv = s_surv)

现在我尝试向 g_rx 对象添加一行。

g <- g_rx + geom_point() + geom_point(data=s, color="black")
g
 Error in eval(expr, envir, enclos) : object 'group' not found 

帮助表示赞赏!

4

1 回答 1

2

一种方法是手动构建您需要的所有对象。这给了你强大的力量。但请记住,权力越大,责任越大。这就是我所做的。我将平均模型的拟合值与生存建模的模型合并rx。我稍微调整了颜色以使平均模型变黑。随意玩弄颜色、线型、尺寸……

library (survival)
library(GGally)
library(ggplot2)
data(colon)

# This generates the overall survival curve (without covariates):
kms_avg <- survfit(Surv(time, status)~1, data =colon)
g_avg <- ggsurv(kms_avg, surv.col="red", xlab="Time (days)", lty.ci=0)

# This generates the survival curve for covariate 'rx'
kms_rx <- survfit(Surv(time, status)~rx, data =colon)
g_rx <- ggsurv(kms_rx, surv.col="red", xlab="Time (days)", lty.ci=0)

s_avg <- summary(kms_avg) 
s_rx <- summary(kms_rx)

s <- data.frame(time = s_avg$time, surv = s_avg$surv, strata = "average")
s_rx <- data.frame(time = s_rx$time, surv = s_rx$surv, strata = s_rx$strata)

xy <- rbind(s, s_rx)

ggplot(xy, aes(x = time, y = surv, color = strata)) +
  theme_bw() +
  geom_point() +
  geom_line()

ggplot(xy, aes(x = time, y = surv, color = strata)) +
  theme_bw() +
  geom_point(size = 0.5) +
  geom_line() +
  scale_color_manual(values = c("black", "#e41a1c", "#377eb8", "#4daf4a"))

在此处输入图像描述

或者,您可以手动定义分组。

g_rx + geom_point(data=s, aes(group = 1), color="black")

在此处输入图像描述

于 2016-06-10T07:57:06.723 回答