Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How change labels name in risk table using ggsurvfit package?

Is it possible to edit the risk table labels?

I’m trying to change the risk table labels (‘male’, ‘female’) to something else.

library(tidyverse)
library(ggsurvfit)

p <- 
  survfit2(Surv(time, status) ~ sex, data = df_lung) %>%
  ggsurvfit(linewidth = 1) + 
  scale_ggsurvfit() + 
  add_risktable(
    risktable_stats = c("n.risk")) +
  theme_classic() +
  scale_color_manual(values = c('blue', 'magenta'),
                     labels = c('My label', 'My other label')) +
  scale_fill_manual(values = c('blue', 'magenta'),
                    labels = c('My label', 'My other label'))


p


enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If I use scale_color_manual and scale_fill_manual, the ggplot legend shows the changed labels but the table shows the output labels. How edyting risk table inside?

>Solution :

It’s easiest to simply change the labels in the data frame before you call survfit2(). But you can also add any ggplot2() functions to the add_risktable(theme) argument. Examples of both below!

library(ggsurvfit)
#> Loading required package: ggplot2
library(tidyverse)
packageVersion("ggsurvfit")
#> [1] '1.0.0'

# it's easiest to change the label in the data before
p <- 
  survfit2(
    Surv(time, status) ~ sex, 
    data = df_lung |> mutate(sex = case_match(sex, "Male" ~ 'My label', "Female" ~ 'My other label'))
  ) %>%
  ggsurvfit(linewidth = 1) + 
  scale_ggsurvfit() + 
  add_risktable(
    risktable_stats = c("n.risk")
  ) +
  theme_classic() +
  scale_color_manual(values = c('blue', 'magenta')) +
  scale_fill_manual(values = c('blue', 'magenta'))
p


# you can also modify the labels directly using ggplot functions 
# in the theme argument of add_risktable()
p <- survfit2(Surv(time, status) ~ sex, data = df_lung) %>%
  ggsurvfit(linewidth = 1) + 
  scale_ggsurvfit() + 
  add_risktable(
    risktable_stats = c("n.risk"),
    theme = 
      list(
        theme_risktable_default(),
        scale_y_discrete(label = c('My other label', 'My label'))
      )
  ) +
  theme_classic() +
  scale_color_manual(values = c('blue', 'magenta'),
                     labels = c('My label', 'My other label')) +
  scale_fill_manual(values = c('blue', 'magenta'),
                    labels = c('My label', 'My other label'))
p

Created on 2024-01-15 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading