一、GGsurvplot包
GGsurvplot是一個基於ggplot2的生存分析可視化工具包,提供了EasySurv和survminer兩個主要函數。該包可以被用於檢查生存分析模型的質量和探究患者在不同因素下的生存概率。GGsurvplot包可以用於多種生存分析,包括Kaplan-Meier與Cox比例風險模型。同時,GGsurvplot包還提供了多種類圖表風格,可以根據需要自由組合。
代碼安裝:
install.packages("devtools")
devtools::install_github("kassambara/ggsurvplot")
二、GGsurvplot函數
ggsurvplot()函數是GGsurvplot包中最常用的函數,經常用於生成生存分析圖表。在使用ggsurvplot函數的時候,需要事先了解生存數據的基本結構,包括患病時間(time)、事件狀態(status)和因素變數(group)。
#數據預處理
library(survminer)
data("lung")
lung$survival[lung$survival=="<5"] <- 4.9 #將<5替換成4.9
lung$survival <- as.numeric(lung$survival) #轉換成數值變數
#生成生存對象
library(survival)
library(survminer)
surv_object <- with(lung,Surv(time, status)) #生成生存對象
#計算組內生存比較(變異)的p值
pval <- survdiff(surv_object~sex, data=lung)$chisq[1]*2
#畫圖
library(ggsurvplot)
ggsurvplot(survfit(surv_object~sex, data=lung),title="Lung cancer \n\nKM-survival",pval=paste("P = ", round(pval, 3)),
legend.labs=c("Male","Female"),ggtheme = theme_bw())
三、GGsurvplot圖片拼在一起
GGsurvplot還提供了許多其它函數來生成各種生存分析圖表,可以對不同的圖表進行組合。這裡介紹如何將多張生存分析圖表拼合到一起。
首先需要生成多個ggsurvplot()圖表:
fit1 <- survfit(surv_object ~ sex, data = lung)
p1 <- ggsurvplot(fit1, legend="none", ylim=c(0.5,1),
ggtheme=theme(panel.background = element_rect(fill = "#f5f5f5")))
fit2 <- survfit(surv_object ~ age, data = lung)
p2 <- ggsurvplot(fit2, legend="none", ylim=c(0.5,1),
ggtheme=theme(panel.background = element_rect(fill = "#f5f5f5")))
fit3 <- survfit(surv_object ~ ph.karno, data = lung)
p3 <- ggsurvplot(fit3, legend="none", ylim=c(0.5,1),
ggtheme=theme(panel.background = element_rect(fill = "#f5f5f5")))
然後,使用gridExtra包的grid.arrange()函數,將圖表拼合在一起。
library(gridExtra)
grid.arrange(p1, p2, p3, ncol=2)
四、GGsurvplot詳解
在生成生存分析圖表時,我們可以對繪圖元素進行高度的自定義。
(1)設置坐標軸:
ggsurvplot(fit, breaks=seq(0,60,10),ncensor.plot=TRUE,
surv.scale = "percent",
risk.table = TRUE,
risk.table.col = "strata",
conf.int = TRUE,
pval = TRUE,
xlim=c(0,60),
ylim=c(0.5,1),
legend="none",
legend.title="Strata",
palette="Set2",
ggtheme=theme_bw(),
xlab="Time (days)",
ylab="Survival Probability",
main="Survival by colon cancer stage",
font.tickslab = 14)
(2)標題居中:
ggsurvplot(fit,
surv.scale = "percent",
risk.table = TRUE,
risk.table.col = "strata",
conf.int = TRUE,
pval = TRUE,
legend="none",
palette="Set2",
ggtheme = theme_classic()+
theme(plot.title = element_text(hjust = 0.5)))
(3)P值小數點位數:
ggsurvplot(fit,
surv.scale = "percent",
risk.table = TRUE,
risk.table.col = "strata",
conf.int = TRUE,
pval = TRUE,
pval.size=8,
pval.coord = c(0.75,0.2),
pval.method = TRUE,
pval.round=3,
legend="none",
palette="Set2",
ggtheme=theme_classic())
五、GGsurvplot改變曲線類型
在生存分析中,我們可以對生存曲線的類型進行高度自定義。
#修改平滑曲線的類型
ggsurvplot(fit2,
palette="Accent",
alpha=0.6,
conf.int = TRUE,
conf.int.style=c("step", "ribbon"), #平滑曲線類型
linetype = "solid",
ggtheme = theme_minimal(),
legend.title = "Strata") +
labs(title = "Effect of sex on survival",
x="Time (days)",
y="Survival probability")
六、小結
通過GGsurvplot包,我們可以非常方便地進行生存分析可視化,同時對生成的圖表進行高度自定義。GGsurvplot提供了多種函數可以使用,同時還支持與ggplot2相似的語法。我們可以很容易地根據需要進行組合,以達到更好的展示效果。
原創文章,作者:IEWCT,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/371470.html