❝本节来介绍一个做生存分析的新包「ggsurvfit」,完美兼容「ggplot2」语法;下面来简单介绍一下,具体请查看作者官方文档 地址:https://github.com/ddsjoberg/ggsurvfit/ ❞
加载R包
代码语言:javascript复制devtools::install_github("ddsjoberg/ggsurvfit")
library(tidyverse)
library(ggsurvfit)
library(gghighlight)
install.packages("tidycmprsk")
library(tidycmprsk)
案例1
代码语言:javascript复制p <- survfit2(Surv(time, status) ~ sex, data = df_lung) %>%
ggsurvfit(size = 1)
add_censor_mark()
add_confidence_interval()
add_risktable(theme=theme_test()
theme(axis.title = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x=element_blank(),
axis.text.y = element_text(color="black",size=10)),
combine_groups=F)
add_quantile(color ="grey80",size=0.8,linetype =5)
❝可以看到语法完全适用于「ggplot2」通过 「 」 来进行图层的叠加,下面来介绍如何修改主题及其它度量参数 ❞
修改主题
代码语言:javascript复制p
theme(legend.position = "bottom",
legend.title = element_blank())
labs(
y = "Probability of survival",
x = "Months since treatment",
title = "Kaplan-Meier Estimate of Survival by Sex")
scale_y_continuous(label = scales::percent, expand = c(0.01, 0))
scale_x_continuous(breaks = 0:5*6, expand = c(0.02, 0))
分面操作
代码语言:javascript复制survfit2(Surv(time, status) ~ sex, data = df_lung) %>%
ggsurvfit(size = 1)
add_censor_mark(shape = 4)
add_quantile(linetype = 3, size = 1)
add_confidence_interval()
facet_grid(~strata)
❝还可进行分面操作,那么这样就会有了更多的施展空间 ❞
高亮显示部分数据
代码语言:javascript复制survfit2(Surv(time, status) ~ ph.ecog, data = df_lung) %>%
ggsurvfit(size = 1)
ggplot2::labs(color = "Gender")
gghighlight::gghighlight(strata == "Asymptomatic", calculate_per_facet = TRUE)
累积发病率
代码语言:javascript复制cuminc(Surv(ttdeath, death_cr) ~ trt, trial) %>%
ggcuminc(outcome = "death from cancer", size = 1)
add_confidence_interval()
add_quantile(y_value = 0.20, size = 1)
add_risktable()
labs(x = "Months Since Treatment")
theme(legend.position = "bottom")
scale_y_continuous(label = scales::percent, expand = c(0.02, 0))
scale_x_continuous(breaks = 0:4 * 6, expand = c(0.02, 0))
❝可以看到使用「ggsurvfit」来进行生存分析图表绘制给了我们更大的操作空间,此包还在不断完善中,具体内容请查看作者官方文档ggsurvfit