生存分析研究的是某個事件發生之前過去的時間,在臨床研究中最常見的應用就是死亡率的估計(預測患者的生存時間),不過生存分析也可以應用于其他領域如機械故障時間等。
在R中,survival包中有很多函數可以對生存數據進行建模,可以使用survfit() 來估計刪失數據的生存曲線,使用coxph()用來擬合Cox 比例風險模型。在survminer包中,可以使用plot()、ggsurvplot()來繪制Kaplan-Meier生存曲線。
今天看到一個不錯的繪制生存曲線的R包——ggsurvfit包,這個包最大的優點就是基于ggplot2語法來繪制生存曲線,相比survminer包的ggsurvplot()函數,ggsurvfit包的可定制化程度更高,而且繪制起來更加簡單,強力推薦。
下面來學習下。
使用survival包的lung數據集,數據集包括了NCCTG晚期肺癌患者的生存率。
運行以下代碼加載數據:
library(survival) # 加載包
head(lung) # 預覽數據

# 數據解釋
inst # 機構代碼;
time # 生存天數;
status # 生存狀態,1為刪失,2為死亡;
age # 年齡;
sex # 性別,1為男性,2為女性;
ph.ecog # 為病人和患者評分,這里用不到;
ph.karno # 為病人和患者評分,這里用不到;
pat.karno # 為病人和患者評分,這里用不到;
meal.cal # 進食時消耗的卡路里;
wt.loss # 最近6個月內的體重下降。
使用survfit2()函數來擬合曲線。
library(ggsurvfit)
fit <- survfit2(Surv(time, status) ~ sex, data = lung)
fit # 可輸出部分信息

將擬合模型轉化為整潔數據框,然后提取其中的數據使用ggplot2包進行繪圖。
生存曲線使用geom_step()函數進行繪制。
library(ggplot2)
fit %>%
tidy_survfit() %>%
ggplot(aes(x = time, y = estimate,
min = conf.low, ymax = conf.low,
color = strata, fill = strata)) +
geom_step()

ggsurvfit包將上述的代碼打包成了一個函數——ggsurvfit()函數,使用ggsurvfit()函數可以輕松繪制生存曲線。
fit %>%
ggsurvfit()

下面我們來修改生存曲線的參數。
因為是使用ggplot2來繪制生存曲線,所以支持ggplot2的語法。
可以使用+號來添加圖層,ggplot2中使用geom_ribbon()來繪制。
不過可以使用add_confidence_interval()函數來添加置信區間。
fit %>%
ggsurvfit() +
add_confidence_interval()

可以使用add_censor_mark()函數來添加刪失點。
fit %>%
ggsurvfit() +
add_censor_mark()

可以使用add_quantile()函數來添加四分位線,默認添加中位線。
可以調整參數來調整線形、大小、顏色等。
fit %>%
ggsurvfit() +
add_quantile(linetype = 2, color = "red")

可以使用add_pvalue()函數來添加統計P值。
也可以使用annotation參數將P值添加在圖形上。
fit %>%
ggsurvfit() +
add_pvalue(caption = "Log-rank {p.value}") +
theme(legend.position = c(0.80, 0.65))

可以使用+號來添加圖層,使用add_risktable()來添加風險表。
可以使用size 參數修改風險表的字體大小。
可以使用risktable_height 參數修改風險表的高度,數字從0到1。
fit %>%
ggsurvfit() +
add_risktable(size = 3,
risktable_height = 0.30) +
theme(legend.position = c(0.80, 0.65))

可以使用add_legend_title()函數來添加圖例標題。
可以使用legend.position參數修改圖例位置。
fit %>%
ggsurvfit() +
add_legend_title(title = "Sex") +
theme(legend.position = c(0.80, 0.65)) +
guides(color = guide_legend(ncol = 2))

上述圖形是使用ggplot2包繪制的,可以使用ggplot2相關的配色。
fit %>%
ggsurvfit() +
add_confidence_interval() +
theme_bw() +
ggsci::scale_fill_lancet()

也可以指定顏色調整圖形顏色。
fit %>%
ggsurvfit() +
add_confidence_interval() +
scale_color_manual(values = c('tomato', 'darkcyan')) +
scale_fill_manual(values = c('tomato', 'darkcyan')) +
theme_bw() +
theme(legend.position = c(0.80, 0.65)) +
guides(color = guide_legend(ncol = 2)) +
labs(
title = "Modified",
y = "Percentage Survival"
)

可以繪制不同的生存曲線,并進行排版。
p <- survfit(Surv(time, status) ~ sex, data = lung) %>%
ggsurvfit() +
add_confidence_interval() +
theme_classic() +
theme(legend.position=c(0.75,0.65)) +
ggsci::scale_fill_nejm()
p1 <- survfit(Surv(time, status) ~ sex, data = lung) %>%
ggsurvfit() +
add_confidence_interval() +
theme_classic() +
theme(legend.position=c(0.75,0.65)) +
ggsci::scale_fill_nejm()
# build plot (which constructs the risktable)
built_p <- ggsurvfit_build(p)
built_p1 <- ggsurvfit_build(p1)
library(patchwork)
wrap_plots(built_p, built_p1, ncol = 2)

對于圖例相同的,也可以合并圖例。
p + p1 +
patchwork::plot_layout(guides = "collect") &
theme(legend.position = "bottom")

參考資料
1.ggsurvfit包幫助文件。