我试图在同一个图中绘制多条生存曲线.使用plot
我可以轻松地做到这一点
plot(sr_fit_0, col = 'red' , conf.int=TRUE, xlim=c(0, max_m)) par(new=TRUE) plot(sr_fit_1, col ='blue', conf.int=TRUE, xlim=c(0, max_m))`
但是现在我想用它ggsurv
来绘制生存曲线,我不知道如何在同一个图中使用它们(不是子图).任何帮助表示赞赏.
为了仓鼠和沙鼠的生命,我在下面生成了一些数据.您可以使用类似于其他曲线拟合函数的survfit()函数,并定义一个分割总体的数据框列.当您使用ggsurv()创建绘图时,我认为它将显示您要查找的内容.
## Make some data for varmint life set.seed(1); l1 <- rnorm(120, 2.5, 1) gerbils <- data.frame(life = l1[l1>0]) set.seed(3); l2 <- rnorm(120, 3, 1) hamsters <- data.frame(life = l2[l2>0]) ## Load required packages require('survival'); require('GGally') ## Generate fits for survival curves ## (Note that Surv(x) creates a Survival Object) sf.gerbils <- survfit(Surv(life) ~ 1, data = gerbils) sf.hamsters <- survfit(Surv(life) ~ 1, data = hamsters) ggsurv(sf.gerbils) #Survival plot for gerbils ggsurv(sf.hamsters) #Survival plot for hamsters ## Combine gerbils and hamsters while adding column for identification varmints <- rbind((cbind(gerbils, type = 'gerbil')), (cbind(hamsters, type = 'hamster'))) ## Generate survival for fit for all varmints as a function of type sf.varmints <- survfit(Surv(life) ~ type, data = varmints) ## Plot the survival curves on one chart ggsurv(sf.varmints)