pharmaverse / ggsurvfit

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

Feature request: Customize `linetype` aes for multiple strata #185

Closed kevndli closed 7 months ago

kevndli commented 7 months ago

Hello! I am graphing KM plots with 4 strata. I want to manually set the linetype aes, as is possible for the colors:

survfit2(Surv(survival_time/30, survival_status) ~ quartile, data = plotdata) %>%
    ggsurvfit(theme = list(theme_classic(), theme(legend.position = "top")), linewidth = .65, **linetype_aes = TRUE**) +
    scale_x_continuous(limits = c(0, 60), breaks = seq(0, 60, 6)) + scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, .2)) + 
    labs(
        x = "Months",
        y = "Overall survival probability",
        title = "Recurrence by Quartile"
    ) +
    **scale_colour_manual(values = c("#575757", "#e39019", "#2a854e", "#1b7ebc"))** +
    theme(plot.title = element_text(hjust = 0.5, size = 10)) +
    add_risktable(
        risktable_stats = "n.risk",
        stats_label = list(n.risk = "Number at Risk"),
    ) + add_pvalue()

Is it possible to do this already and if not could this feature be added? I think it would be really helpful to differentiate besides with only color and be more customizable!

Thanks for your consideration and look forward to your response!

ddsjoberg commented 7 months ago

Dear @kevndli , thank you for the post!

Can you update your post with a minimal reproducible example (reprex). This is code and data we can run on our machines to illustrate your issue.

Can you also please your post with an example on how you specify linetypes in vanilla ggplot?

kevndli commented 7 months ago

Dear @kevndli , thank you for the post!

Can you update your post with a minimal reproducible example (reprex). This is code and data we can run on our machines to illustrate your issue.

Can you also please your post with an example on how you specify linetypes in vanilla ggplot?

Yes, sorry about that! I currently do not specify linetypes besides setting the linetype_aes argument to TRUE.

library(ggsurvfit)

n <- 100
SDI_quartile <- sample(c("Q1", "Q2", "Q3", "Q4"), n, replace = TRUE)
survival_time <- sample(1:999, n, replace = TRUE) 
survival_status <- sample(c(0, 1), n, replace = TRUE)
plotdata <- data.frame(SDI_quartile, survival_time, survival_status)

survfit2(Surv(survival_time/30, survival_status) ~ SDI_quartile, data = plotdata) %>%
  ggsurvfit(theme = list(theme_classic(), theme(legend.position = "top")), linewidth = .65, linetype_aes = TRUE) +
  labs(
    x = "Months",
    y = "Overall survival probability",
    title = "Recurrence by Quartile"
  ) +
  scale_colour_manual(values = c("#575757", "#e39019", "#2a854e", "#1b7ebc")) +
  theme(plot.title = element_text(hjust = 0.5, size = 10)) +
  add_risktable(
    risktable_stats = "n.risk",
    stats_label = list(n.risk = "Number at Risk"),
  ) + add_pvalue()

Essentially I am wondering if it's possible to manually define the linetypes used on the plot (for example, Q4 to be solid, Q1-3 to be dashed)?

ddsjoberg commented 7 months ago

it's probably possible. but i don't know the code by heart. if you provide the code on how you do this in typical ggplot code (outside of ggsurvfit), then i'll check if it can be done in ggsurvfit

kevndli commented 7 months ago

I think I figured it out! Adding scale_linetype_manual(values = c("dashed", "dotted", "dotdash", "solid")) seems to let you manually set the linetypes.