我拥有的是使用 R 对机械心脏支持患者进行的 Kaplan-Meier 分析。
我需要的是将以下数据添加到图中(如示例中所示):
换句话说,有两组,其中一组是另一组(所有患者)的子集(移植患者)。这两条曲线必须从 0/0 开始并且会增加。
我自己的情节是由:
pump <- read.table(file=datafile, header=FALSE,
col.names=c('TIME', 'CENSUS', 'DEVICE'))
# convert days to months
pump$TIME <- pump$TIME/(730/24)
mfit.overall <- survfit(Surv(TIME, CENSUS==0) ~ 1, data=pump)
plot(mfit.overall, xlab="months on device", ylab="cum. survival", xaxt="n")
axis(1, at=seq(from=0, to=24, by=6), las=0)
如何添加两条附加曲线?
亲切的问候
约翰
样本 Kaplan Meier 曲线:http://i.stack.imgur.com/158e8.jpg
演示数据:
生存数据,进入泵:
TIME CENSUS DEVICE 426 1 1 349 1 1 558 1 1 402 1 1 12 0 1 84 0 1 308 1 1 174 1 1 315 1 1 734 1 1 544 1 2 1433 1 2 1422 1 2 262 1 2 318 1 2 288 1 2 1000 1 2
发送数据:
TIME CENSUS DEVICE 426 1 1 288 1 2 308 1 1
死亡人数:
TIME CENSUS DEVICE 12 0 1 84 0 1
最佳答案
与 par(new=TRUE)
您可以在与第一个图形相同的图形中绘制第二个图形。
通常我会推荐使用 lines()
用于向绘图添加曲线,因为 par(new=TRUE)
执行覆盖图的不同的任务。当以不打算使用的方式使用函数时,您可能会犯错误,例如我差点忘了重要的 xlim
争论。然而,从 survfit
中提取曲线并非易事。对象,所以我认为这是两害相权取其轻的。
# Fake data for the plots
pump <- data.frame(TIME=rweibull(40, 2, 20),
CENSUS=runif(40) < .3,
DEVICE=rep(0:1, c(20,20)))
# load package
library("survival")
# Fit models
mfit.overall <-survfit(Surv(TIME, CENSUS==0) ~ 1, data=pump)
mfit.htx <- survfit(Surv(TIME, CENSUS==0) ~ 1, data=pump, subset=DEVICE==1)
# Plot
plot(mfit.overall, col=1, xlim=range(pump$TIME), fun=function(x) 1-x)
# `xlim` makes sure the x-axis is the same in both plots
# `fun` flips the curve to start at 0 and increase
par(new=TRUE)
plot(mfit.htx, col=2, xlim=range(pump$TIME), fun=function(x) 1-x,
ann=FALSE, axes=FALSE, bty="n") # This hides the annotations of the 2nd plot
legend("topright", c("All", "HTX"), col=1:2, lwd=1)
关于r - Kaplan-Meier 包括生存和移植数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13359241/