admin管理员组

文章数量:1122832

I am using ggsurvfit package to create KM plots. I am having one grouping variable and looking to add legend for that variable.

Please check code below: I am looking to add legend to this particular plot.

library(ggsurvfit)
survfit2(Surv(time, status) ~ 1, data = df_colon) %>%
  ggsurvfit(color = "#508050") +
  add_confidence_interval(fill = "#508050") +
  add_risktable() +
  scale_ggsurvfit()

How can we add legend in the above-mentioned km plot?

I am using ggsurvfit package to create KM plots. I am having one grouping variable and looking to add legend for that variable.

Please check code below: I am looking to add legend to this particular plot.

library(ggsurvfit)
survfit2(Surv(time, status) ~ 1, data = df_colon) %>%
  ggsurvfit(color = "#508050") +
  add_confidence_interval(fill = "#508050") +
  add_risktable() +
  scale_ggsurvfit()

How can we add legend in the above-mentioned km plot?

Share Improve this question edited Nov 21, 2024 at 17:40 stefan 123k6 gold badges36 silver badges72 bronze badges Recognized by R Language Collective asked Nov 21, 2024 at 17:11 Bharath kumar TAMATAMALABharath kumar TAMATAMALA 111 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 3

A simple way would be to add aes() to the plot, mapping color and fill aesthetics to the string you wish to appear in the legend. Again, include manual scales to specify the actual colors you wish to appear.

survfit2(Surv(time, status) ~ 1, data = df_colon) %>%
  ggsurvfit() +
  add_confidence_interval() +
  add_risktable() +
  scale_ggsurvfit() + 
  aes(color = "Your label here", fill = "Your label here") +
  scale_color_manual(NULL, values = "#508050") +
  scale_fill_manual(NULL, values = "#508050") +
  theme(legend.position = "right")

本文标签: rHow to add legend when we have one grouping variable in KM PlotsStack Overflow