I like to obtain graphically the area under a curve de Kaplan-Meier. I have done the previous recommendation in this stack overflow but it doesn't work correctly
Example
library(survival)
data (cancer)
veteran
km <- survfit(Surv(time,status)~1, data=leukemia)
plot(km, conf.int=F, xlab="time until relapse (in weeks)", mark.time = T,
ylab="proportion without relapse",lab=c(10,10,7),col='blue')
abline(h=0)
abline(v=0)
time <- km$time[km$time <= 30]
surv <- km$surv[km$time <= 30]
polygon(
c(min(time), time, max(time)),
c(0, surv, 0),
col = "blue", border = F
)
library(ggplot2)
dat <- data.frame(
time = km$time,
surv = km$surv
)
ggplot(dat, aes(time, surv)) +
geom_line() +
geom_vline(xintercept = 40, color = "blue") +
geom_area(data = subset(dat, time <= 40), fill = "blue") +
theme_minimal()
I would like to obtain graphically the area under curve between 0 and 40, AND between 20 and 60.
Thanks in advance
The issue is that your data lacks the exact time points. Hence, to achieve your desired result you need an estimate or prediction for these time points. As your KM curve is piecewise-linear you could use linear interpolation e.g. using
approx()
: