pharmaverse / ggsurvfit

http://www.danieldsjoberg.com/ggsurvfit/
Other
67 stars 19 forks source link

Feature request: Add confidence intervals to add_quantile() #148

Closed tobiasmuetze closed 1 year ago

tobiasmuetze commented 1 year ago

The add_quantile() function allows to add, for example, the median survival time to a plot. The survfit object not only includes the median survival time, but also the corresponding confidence interval.

The add_quantile() function could be amended to enable adding the confidence intervals for the median survival time (or any other quantile survival time) to a plot.

ddsjoberg commented 1 year ago

Dear @tobiasmuetze , Thank you for the post. Can you link to published examples with the CI on the quantile? I want to know what you have in mind.

tobiasmuetze commented 1 year ago

Thanks, @ddsjoberg, for the quick response. I don't have a published example, but I did it manually now. Please see the example code below.

library(ggsurvfit)

# Run Kaplan-Meier analysis
km_fit <- survfit2(Surv(time = AVAL, event = CNSR==0) ~ 1, 
                   data = adtte, 
                   conf.type="plain")

# Get median survival times and CI
medsurv <- summary(km_fit)$table[["median"]] 
medsurv_lcl <- summary(km_fit)$table[["0.95LCL"]] 
medsurv_ucl <- summary(km_fit)$table[["0.95UCL"]] 

# Idea 1: Highlight CI of median survival time through shaded area
km_fit %>% 
  ggsurvfit() +
  add_quantile(y_value = 0.5, linetype = "dashed", color = "black", linewidth = 0.8) +
  geom_rect(aes(xmin = medsurv_lcl, xmax = medsurv_ucl, ymin = 0, ymax = 0.5), 
            col = NA, alpha = 0.005, fill = "steelblue1")

# Idea 2: Highlight CI of median survival time through horizontal bar
km_fit %>% 
  ggsurvfit() +
  geom_errorbarh(aes(xmin = medsurv_lcl, xmax = medsurv_ucl, y = 0.5, height = .02)) 
ddsjoberg commented 1 year ago

Thank you so much for the thoughtful response.

I feel somewhat torn about implementing this: we are focused on making it dead-simple to create figures for publication. I don't think there is a standard way to display CIs for quantiles, so I think I will need to refrain from implementing for now.

BUT, your example illustrate one of my favorite features of the package....you can very easily add ANYTHING you want to the ggplot using the standard ggplot functions!