将总生存曲线添加到协变量的存活曲线图中

时间:2016-06-10 03:03:47

标签: r ggplot2 ggally

我一直试图添加'整体'生存曲线与给定协变量产生的生存曲线。在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 

帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

一种方法是手工构建所需的所有对象。这给你很大的力量。但请记住,强大的力量带来了巨大的责任。这就是我的所作所为。我将平均模型的拟合值与生存模型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"))

enter image description here

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

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

enter image description here