> setwd(dir="D:/onedrive/Desktop/xxx/数据")
> data<-read.csv("GGL.csv",header=T)
> GGL<-data
> str(GGL)
'data.frame': 365 obs. of 3 variables:
$ time : num 24.3 24.3 24.8 25.3 25.9 ...
$ status: int 0 0 1 0 0 0 0 0 0 0 ...
$ group : num 0 0 1 0 1 0 0 0 0 0 ...
对变量转换成因子,并添加标签
> GGL$group<-factor(GGL$group,levels=c(0,1),labels=c("LPA","Non-LPA"))
> library("survival")
> library("survminer")
用函数Surv() 创建生存对象:
> surv.obj <- Surv(time = GGL$time, event = GGL$status)
> surv.obj
[1] 24.26000+ 24.33000+ 24.79000 25.28000+ 25.87000+ 25.97000+
[7] 26.33000+ 26.63000+ 26.76000+ 27.02000+ 27.06000+ 27.48000+
生存率的K-M估计的计算用函数survfit() 实现:
> ggl <- surv_fit(surv.obj ~ group, data = GGL)
> plot(ggl)
survminer包中的ggsurvplot()函数以ggplot2的风格提供更加丰富的图形输出:
> ggsurvplot(ggl, data = GGL, palette = c("blue", "red"), xlab = "Time (Months)", ylab = "Overall survival (percentage)", break.x.by = 10, break.y.by = 0.25, ylim = c(0.5, 1), xlim = c(0, 100), legend.title = "Histological types", size = 1.2, pval = T, pval.size = 5, pval.coord=c(17.5,0.05), censor.shape=124, censor.size=2, font.main = c(16, "bold", "darkblue"),font.x = c(14, "bold", "black"), font.y = c(14, "bold", "black"), font.tickslab = c(12,"bold", "black"), font.legend = c(12,"bold","black"), risk.table =T, tables.height = 0.25,legend.labs=c("LPA","Non-LPA"),legend = c(0.3,0.3),fontsize=4.5,conf.int=T,conf.int.style='ribbon', conf.int.alpha=0.1,surv.median.line="hv")
如图