Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shade the area under a Kaplan-Meier survival curve?

How can I shade or fill the area under a Kaplan-Meier survival curve ?

Let's draw such a minimal working example with R:

library (survival)
fit2 <- survfit(Surv(entry, futime, death) ~1, myeloma)
plot (fit2, conf.int = FALSE)
abline (v = 3650, col = "blue")

Kaplan-Meier survival curve

I would like to shade or fill the area under the curve between 0 and 3650 days (10 years). I know how to do this when there are two arms with the survRM2 package, but I cannot figure out to when there is only one arm. I want to represent the restricted mean survival which I can easily compute with the following lines:

survival:::survmean (fit2, rmean = 3650)
$matrix
   records      n.max    n.start     events      rmean  se(rmean)     median    0.95LCL    0.95UCL 
3882.00000 2387.00000 2194.00000 2769.00000 1108.22471   19.62718  764.00000  728.00000  811.00000 

$end.time
[1] 3650

Base R or ggplot2 solutions are both welcome.

Thank you in advance.

Charles

like image 937
CharlesLDN Avatar asked Nov 18 '25 11:11

CharlesLDN


1 Answers

Using base R you could fill the area under the curve using polygon:

library(survival)
fit2 <- survfit(Surv(entry, futime, death) ~ 1, myeloma)
plot(fit2, conf.int = FALSE)
abline(v = 3650, col = "blue")

time <- fit2$time[fit2$time <= 3650]
surv <- fit2$surv[fit2$time <= 3650]

polygon(
  c(min(time), time, max(time)),
  c(0, surv, 0),
  col = "blue", border = F
)

Or using ggplot2 and a geom_area you could do:


library(ggplot2)

dat <- data.frame(
  time = fit2$time,
  surv = fit2$surv
)

ggplot(dat, aes(time, surv)) +
  geom_line() +
  geom_vline(xintercept = 3650, color = "blue") +
  geom_area(data = subset(dat, time <= 3650), fill = "blue") +
  theme_minimal()

like image 170
stefan Avatar answered Nov 19 '25 23:11

stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!