服务热线
178 0020 3020
R语言之画生存曲线
任务:ggplot2拓展包画生存曲线
install.packages("survival") install.packages("survminer") library(survival) library(survminer) library(ggplot2) library(ggpubr) library(magrittr) fit<- survfit(Surv(time, status) ~ sex, data = lung) ggsurvplot(fit, data = lung,title="R2-03-12", surv.median.line = "hv", ##中值生存中的水平/垂直线 legend.title = "SEX", legend.labs = c("Male", "Female"), pval = TRUE,conf.int = TRUE, conf.int.alpha=0.1,##设置图上置信区间颜色的透明度,取值范围0-1,0相当于看不到,1就是不透明(R2-29) risk.table = "abs_pct",##显示绝对数量和风险个体的百分比 risk.table.title ="R2-03-12", risk.table.col="sex", risk.table.y.text.col=TRUE,risk.table.y.text=FALSE,##在风险表的文本注释中提供条而不是名称 ncensor.plot=TRUE,##绘制在时间t的审查主题的数量 ncensor.plot.title="R2-03-12", tables.height = 0.22,##默认值是0.25 break.time.by=200,##在时间间隔中将x轴断开200 palette = c("red","blue"))
拓展:Log-Rank检验比较生存曲线:survdiff()
surv_diff<-survdiff(Surv(time,status)~sex,data=lung) surv_diff require('survival') fit2<-survfit(Surv(time,status)~sex+rx+adhere,data = colon) ggsurv<-ggsurvplot(fit2,fun='event',conf.int = TRUE,ggtheme = theme_bw()) ggsurv$plot+theme_bw()+theme(legend.position = 'right')+facet_grid(rx~adhere)
下面的图显示了性别变量根据rx&adhere的值生存的曲线:
延伸:使用“survival”包和“KMsurv”包来绘制生存曲线
install.packages("KMsurv") library(survival) library(KMsurv) myfit1 = survfit( Surv(time,status) ~ sex,data=lung) plot(myfit1,col=c("red","blue"), conf.int = c("both"),##该参数有"both","only","none"三个值可选。 main="Kaplan-Meier estimate ", xlab="time", ylab="survival function for different sex group") legend('topright',legend=c("Male","Female"),lty = 1, lwd = 2.5, cex= 0.8, col = c("red","blue"),pt.cex = 1.25,bty = "n",title = "Sex")
附件