如何使用ggsurvfit包更改风险表中的标签名称?

问题描述 投票:0回答:1

可以编辑风险表标签吗?

我正在尝试将风险表标签(“男性”、“女性”)更改为其他内容。

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

如果我使用scale_color_manual和scale_fill_manual,ggplot图例显示更改的标签,但表格显示输出标签。里面的风险表怎么编辑?

r label survival-analysis survival ggsurvfit
1个回答
0
投票

最简单的方法是在调用

survfit2()
之前更改数据框中的标签。但您也可以将任何
ggplot2()
函数添加到
add_risktable(theme)
参数中。以下两个示例!

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

创建于 2024-01-15,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.