本文介绍了用ggplot2绘制R中的生存曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直在寻找一个解决方案,用ggplot2来绘制生存曲线。我发现了一些很好的例子,但它们并没有遵循整个ggplot2美学(主要关于阴影置信区间等)。所以最后我写了自己的函数: pre $ ggsurvplot< -function(s,conf.int = T,events = T ,shape =|,xlab =Time, ylab =生存概率,zeroy = F,col = T,linetype = F){ #s:目的。 #conf.int:TRUE或FALSE来绘制置信区间。 #events:发生审查事件时绘制点的真或假 #shape:这些点的形状 #zeroy:强制y轴达到0 #col: TRUE,FALSE或带颜色的矢量。颜色或B / W #linetype:TRUE,FALSE或具有线条类型的矢量。 $ b $ require(ggplot2) require(存活) if(class(s​​)!=survfit)stop(Survfit object required) #用所有数据生成一个数据帧 sdata< -data.frame(time = s $ time,surv = s $ surv,lower = s $ lower,upper = s $ upper) sdata $ strata< -rep(names(s $ strata),s $ strata) #创建一个空白画布 kmplot< -ggplot(sdata,aes(x =时间y =幸存))+ geom_blank()+ xlab(xlab)+ ylab(ylab)+ theme_bw() #设置调色板 if(is.logical(col))ifelse(col, kmplot< -kmplot + scale_colour_brewer(type =qual,palette = 6)+ scale_fill_brewer(type =qual,调色板= 6), kmplot< -kmplot + scale_colour_manual(values = rep(black,length(s $ strata)))+ scale_fill_manual(values = rep(black,length(s $ strata))) )) else kmplot< -kmplot + scale_fill_manual(values = col)+ scale_colour_manual(values = col) #设置线型 if(is.logi cal(linetype))ifelse(linetype, kmplot< -kmplot + scale_linetype_manual(values = 1:length(s $ strata)), kmplot< -kmplot + scale_linetype_manual(values = rep(1,length (s $ strata)))) else kmplot< -kmplot + scale_linetype_manual(values = linetype) #强制y轴归零 if(zeroy) { kmplot< -kmplot + ylim(0,1)} #置信区间 if(conf.int){ #用阶梯线创建数据帧n ys xs scurve.step< -data.frame(time = sdata $ time [xs],lower = sdata $ lower [ys],upper = sdata $ upper [ys],surv = sdata $ surv [ys],strata = sdata $ strata [ys]) kmplot< -kmplot + geom_ribbon(data = scurve.step,aes(x = time,ymin =如果(事件){ kmplot< -kmplot + $ b $,下面的,ymax = upper,fill = strata),alpha = 0.2)} #Events b geom_point(aes(x = time,y = surv,col = strata),shape = shape)} #存活加强线 kmplot< -kmplot + geom_step( data = sdata,aes(x = time,y = surv,col = strata,linetype = strata)) #返回ggplot2对象 kmplot } 我为每个阶层使用for循环编写了以前的版本,但速度较慢。由于我不是程序员,因此我寻求改进功能的建议。也许在危险的患者中添加数据表,或者在ggplot2框架中实现更好的集成。 感谢 (我在这里使用开发版本,因为这里有一个在生产版本中缺少参数 alpha (对于非默认值不会正确地遮蔽上部矩形),否则功能相同)。 库(devtools) dev_mode(TRUE)#如果您不想永久安装 install_github(survMisc,dardisco) library(survMisc,lib.loc =C:/ Users / c / R-dev)#无论你在/ devtools把它放在哪里数据(肾脏,包装=KMsurv) p1 type =fill,存活大小= 2 ,palette =Pastel1, fillLineSize = 0.1,alpha = 0.4)$ plot p1 + theme_classic() dev_mode(FALSE) 给出: 对于一个经典的情节和表格: pre> autoplot(autoplot(survfit(Surv(time,delta)〜type,data = kidney), type =CI)) 参见?survMisc :: a utoplot.survfit 和?survMisc :: autoplot.tableAndPlot 获得更多选项。 I've been looking for a solution to plot survival curves using ggplot2. I've found some nice examples, but they do not follow the whole ggplot2 aesthetics (mainly regarding shaded confidence intervals and so on). So finally I've written my own function:ggsurvplot<-function(s, conf.int=T, events=T, shape="|", xlab="Time", ylab="Survival probability", zeroy=F, col=T, linetype=F){#s: a survfit object.#conf.int: TRUE or FALSE to plot confidence intervals.#events: TRUE or FALSE to draw points when censoring events occur#shape: the shape of these points#zeroy: Force the y axis to reach 0#col: TRUE, FALSE or a vector with colours. Colour or B/W#linetype: TRUE, FALSE or a vector with line types.require(ggplot2)require(survival)if(class(s)!="survfit") stop("Survfit object required")#Build a data frame with all the datasdata<-data.frame(time=s$time, surv=s$surv, lower=s$lower, upper=s$upper)sdata$strata<-rep(names(s$strata), s$strata)#Create a blank canvaskmplot<-ggplot(sdata, aes(x=time, y=surv))+ geom_blank()+ xlab(xlab)+ ylab(ylab)+ theme_bw()#Set color paletteif(is.logical(col)) ifelse(col, kmplot<-kmplot+scale_colour_brewer(type="qual", palette=6)+scale_fill_brewer(type="qual", palette=6), kmplot<-kmplot+scale_colour_manual(values=rep("black",length(s$strata)))+scale_fill_manual(values=rep("black",length(s$strata))) )else kmplot<-kmplot+scale_fill_manual(values=col)+scale_colour_manual(values=col)#Set line typesif(is.logical(linetype)) ifelse(linetype, kmplot<-kmplot+scale_linetype_manual(values=1:length(s$strata)), kmplot<-kmplot+scale_linetype_manual(values=rep(1, length(s$strata))) )else kmplot<-kmplot+scale_linetype_manual(values=linetype)#Force y axis to zeroif(zeroy) { kmplot<-kmplot+ylim(0,1)}#Confidence intervalsif(conf.int) { #Create a data frame with stepped lines n <- nrow(sdata) ys <- rep(1:n, each = 2)[-2*n] #duplicate row numbers and remove the last one xs <- c(1, rep(2:n, each=2)) #first row 1, and then duplicate row numbers scurve.step<-data.frame(time=sdata$time[xs], lower=sdata$lower[ys], upper=sdata$upper[ys], surv=sdata$surv[ys], strata=sdata$strata[ys]) kmplot<-kmplot+ geom_ribbon(data=scurve.step, aes(x=time,ymin=lower, ymax=upper, fill=strata), alpha=0.2)}#Eventsif(events) { kmplot<-kmplot+ geom_point(aes(x=time, y=surv, col=strata), shape=shape)}#Survival stepped linekmplot<-kmplot+geom_step(data=sdata, aes(x=time, y=surv, col=strata, linetype=strata))#Return the ggplot2 objectkmplot}I wrote a previous version using for loops for each strata, but is was slower. As I'm not a programmer, I look for advice to improve the function. Maybe adding a data table with patients at risk, or a better integration in the ggplot2 framework.Thanks 解决方案 You could try the following for something with shaded areas between CIs:(I'm using the development version here as there's a flaw with the parameter alpha in the production version (doesn't shade upper rectangles correctly for non-default values). Otherwise the functions are identical). library(devtools)dev_mode(TRUE) # in case you don't want a permanent installinstall_github("survMisc", "dardisco")library("survMisc", lib.loc="C:/Users/c/R-dev") # or wherever you/devtools has put itdata(kidney, package="KMsurv")p1 <- autoplot(survfit(Surv(time, delta) ~ type, data=kidney), type="fill", survSize=2, palette="Pastel1", fillLineSize=0.1, alpha=0.4)$plotp1 + theme_classic()dev_mode(FALSE)giving:And for a classic plot and table:autoplot(autoplot(survfit(Surv(time, delta) ~ type, data=kidney), type="CI"))See ?survMisc::autoplot.survfit and ?survMisc::autoplot.tableAndPlot for more options. 这篇关于用ggplot2绘制R中的生存曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-05 21:03