一、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/n/371470.html