user23416006
user23416006

Reputation: 45

Is there a way to control the ordering of groups in ggcuminc?

When creating a plot with ggcuminc, I can't find a way to change the automatic alphabetical ordering of the levels

trial2 <- trial |> 
mutate(grade2 = fct_recode(grade, one = "I", two = "II", three = "III"))

levels(trial2$grade2)
# [1] "one"   "two"   "three"

p1 <- cuminc(Surv(ttdeath, death_cr)~grade, data = trial2) |> 
  ggcuminc()

p2 <- p1 <- cuminc(Surv(ttdeath, death_cr)~grade, data = trial2) |> 
  ggcuminc()

Is there a way of changing the levels of the grade2 plot (p2) so that the legend shows "one - two - three" and not "one - three - two" ? (see image and compare p1 and p2)

enter image description here

Upvotes: 0

Views: 72

Answers (1)

stefan
stefan

Reputation: 123893

One option to achieve your desired result would be to set the order via the limits= argument of the color scale, i.e. scale_color_discrete for the default color scale:

library(tidyverse)
library(ggsurvfit)
library(tidycmprsk)
#> Registered S3 method overwritten by 'tidycmprsk':
#>   method                    from     
#>   global_pvalue_fun.tidycrr gtsummary

trial2 <- trial |>
  mutate(grade2 = fct_recode(grade, one = "I", two = "II", three = "III"))

cuminc(Surv(ttdeath, death_cr) ~ grade2, data = trial2) |>
  ggcuminc() +
  scale_color_discrete(limits = levels(trial2$grade2))
#> Plotting outcome "death from cancer".

Upvotes: 1

Related Questions