kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
497 stars 161 forks source link

How to wrap a coxph model for plotting #487

Open jvanlunenburg opened 4 years ago

jvanlunenburg commented 4 years ago

I'm having trouble plotting the survival curves for a 2-group factor (sex) using the coxph fit.

coxph(Surv(time = event.days, event = outcome.status) ~ sex, data = df) %>% 
    survfit(data = df) %>% 
    ggsurvplot

The above gives a single line for overall survival. I want 2 lines, one per group.

Error in UseMethod("group_by_") : 
  no applicable method for 'group_by_' applied to an object of class "NULL"
In addition: Warning message:
`group_by_()` is deprecated as of dplyr 0.7.0.
Please use `group_by()` instead.
jvanlunenburg commented 4 years ago

Update: I discovered the ggadjustedcurve function was made for this purpose. The only example I found used outdated syntax, but I eventually got it working after finding out that data can't be a tibble (got a cryptic sorting bug).

I just don't understand why another function was necessary, ideally I'd just pass my coxph object to ggsurvplot.

fowcai commented 3 years ago

I found this thread after attempting to customize my coxph plot and realizing that it doesn't seem possible to use the facet.by=" " or ggsurvplot_facet_by() functions when plotting a coxph model.

My goal is to display a curve for each genotype (WT, Tg) on the same plot (as shown below), and then "facet" by sex, so I have a mini-panel for Males (with WT, Tg shown), and another panel for females (also showing curves for WT and Tg). I could show all four groups (WTM, WTF, TgM, TgF) on the same plot, but it is messy so I'd like to try to split them. It seems as though the ggadjustedcurve function is made for plotting adjusted coxph models, but doesn't support the facet.by functionality.

The model I want to plot, along with my attempt to plot it, are shown here:

coxplot_geno0 = coxph(Surv(latency,success) ~ strata(genotype) + sex + age + speed + trial, data = df2_geno0)

ggsurvplot(surv_fit(coxplot_geno0, data = df2_geno0),
          conf.int=TRUE, conf.int.alpha=0.15,
          fun = "event", surv.median.line = "hv", cumevents = FALSE, cumcensor = FALSE, 
          palette = mycolours_geno, xlim=c(0,180), break.time.by=30, surv.scale = "percent",
          ggtheme = theme_classic(base_size = 14), legend.title ="") +
          labs(x = "Time(s)", y = "Proportion Escaped", title = "Timepoint 0 - 4 months") 

Thanks very much!!

Screen Shot 2020-11-11 at 2 36 13 PM