pharmaverse / ggsurvfit

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

Change risk table order #213

Open saskia-c opened 1 month ago

saskia-c commented 1 month ago

When plotting a cuminc() plot and adding a risk table, the order of the strata in the risk table is alphabetical. Is it possible for the order to follow the levels of a factor?

In the example below, I would like Drug B to be the top row in the risk table, and Drug A to come second. It seems by default the risk table does not follow the levels of the factored stratum.


# data
trial_new <- trial %>%
  mutate(trt = factor(trt,
                      levels = c("Drug B", "Drug A"),
                      labels = c("Drug B", "Drug A")),
         cr_response = ifelse(response == 1, 1, death*2))

# plot
cuminc(Surv(ttdeath, factor(cr_response)) ~ trt, data = trial_new) %>%
  ggcuminc() +
  add_risktable(risktable_stats = "{n.risk} ({cum.event})") 

Not sure if this a bug or a missing feature, but any help on this would be greatly appreciated. Many thanks!

ddsjoberg commented 1 month ago

Dear @saskia-c , That seems to be a very reasonable expectation! Thanks for the post