将总体生存曲线添加到协变量的生存曲线图中
Add overall survival curve to survival curve plot for a covariate
我一直在尝试将 'overall' 生存曲线添加到为给定协变量生成的生存曲线中。该曲线是使用 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
感谢帮助!
一种方法是手动构建您需要的所有对象。这会给你很大的力量。但请记住,能力越大,责任越大。这就是我所做的。我将平均模型的拟合值与根据 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")
我一直在尝试将 'overall' 生存曲线添加到为给定协变量生成的生存曲线中。该曲线是使用 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
感谢帮助!
一种方法是手动构建您需要的所有对象。这会给你很大的力量。但请记住,能力越大,责任越大。这就是我所做的。我将平均模型的拟合值与根据 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")