本文介绍了在ggplot或lattice中使用Surv对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 任何人都知道如何利用ggplot或lattice来进行生存分析?这样做会很好,可以做一个网格或类似生存的图表。 所以最后我玩过并排序找到了Kaplan-Meier图的解决方案。我很抱歉将列表元素放入一个数据框中,但我无法找到另一种方式。 注意:它只适用于两层地层。如果有人知道我可以如何使用 x 来做到这一点,请让我知道(在Stata中,我可以附加到宏 - 不确定这是如何工作在R )。 ggkm m2s fit< - 幸存(m2s〜阶层) f $时间< - 适合$时间 f $ surv< - fit $ surv f $ strata< - c(rep(names(fit $ strata [1]),fit $ strata [1]), rep (名称(fit $ strata [2]),fit $ strata [2])) f $ upper f $ lower r + geom_line()+ geom_ribbon(aes(ymin =低,ymax =高),alpha = 0.3) return(r)} 解决方案我一直在 lattice 中使用下面的代码。第一个函数为一组绘制KM曲线,通常用作 panel.group 函数,而第二个函数为整个绘图添加log-rank测试p值面板: km.panel na.part< - is.na(x)| is.na(y)x< - x [!na.part] y< - y [!na。 ($) if(length(x)== 0)return() fit< - survfit(Surv(x,y)〜1) if(mark.time){ cens panel.xyplot(fit $ time [cens],fit $ surv [cens],type =p ,...)} panel.xyplot(c(0,fit $ time),c(1,fit $ surv),type =s,...) } logrank.panel< - 函数(x,y,下标,组,...){ lr otmp etmp df 0))) - 1 p p.text< - paste(p =,signif(p,2)) grid.text(p.text,0.95,0.05,just = c(right,bottom)) panel.superpose(x = x,y = y,下标=下标,组= group,...)} 审查指标必须为0-1为此代码工作。用法如下: library(生存)库(格)库(网格)数据(冒号)#内置示例数据集 xyplot(status_time,data = colon,groups = rx,panel.groups = km.panel,panel = logrank.panel) 如果您只是使用'panel = panel.superpose',那么您将无法获得p值。 Anyone knows how to take advantage of ggplot or lattice in doing survival analysis? It would be nice to do a trellis or facet-like survival graphs.So in the end I played around and sort of found a solution for a Kaplan-Meier plot. I apologize for the messy code in taking the list elements into a dataframe, but I couldnt figure out another way. Note: It only works with two levels of strata. If anyone know how I can use x<-length(stratum) to do this please let me know (in Stata I could append to a macro-unsure how this works in R).ggkm<-function(time,event,stratum) { m2s<-Surv(time,as.numeric(event)) fit <- survfit(m2s ~ stratum) f$time <- fit$time f$surv <- fit$surv f$strata <- c(rep(names(fit$strata[1]),fit$strata[1]), rep(names(fit$strata[2]),fit$strata[2])) f$upper <- fit$upper f$lower <- fit$lower r <- ggplot (f, aes(x=time, y=surv, fill=strata, group=strata)) +geom_line()+geom_ribbon(aes(ymin=lower,ymax=upper),alpha=0.3) return(r)} 解决方案 I have been using the following code in lattice. The first function draws KM-curves for one group and would typically be used as the panel.group function, while the second adds the log-rank test p-value for the entire panel: km.panel <- function(x,y,type,mark.time=T,...){ na.part <- is.na(x)|is.na(y) x <- x[!na.part] y <- y[!na.part] if (length(x)==0) return() fit <- survfit(Surv(x,y)~1) if (mark.time){ cens <- which(fit$time %in% x[y==0]) panel.xyplot(fit$time[cens], fit$surv[cens], type="p",...) } panel.xyplot(c(0,fit$time), c(1,fit$surv),type="s",...)}logrank.panel <- function(x,y,subscripts,groups,...){ lr <- survdiff(Surv(x,y)~groups[subscripts]) otmp <- lr$obs etmp <- lr$exp df <- (sum(1 * (etmp > 0))) - 1 p <- 1 - pchisq(lr$chisq, df) p.text <- paste("p=", signif(p, 2)) grid.text(p.text, 0.95, 0.05, just=c("right","bottom")) panel.superpose(x=x,y=y,subscripts=subscripts,groups=groups,...)}The censoring indicator has to be 0-1 for this code to work. The usage would be along the following lines:library(survival)library(lattice)library(grid)data(colon) #built-in example data setxyplot(status~time, data=colon, groups=rx, panel.groups=km.panel, panel=logrank.panel)If you just use 'panel=panel.superpose' then you won't get the p-value. 这篇关于在ggplot或lattice中使用Surv对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-25 14:38