rggplot2survminer

Reposition risk table labels in ggsurvplot


-- Possible SOLUTION as answer

The labels are quite long resulting in shifting the No at risk to the right.
Further, they are not left-aligned anymore.
Thus, they are not in line with the x-axis of the plot.
How to reposition the strata labels on top, above the number at risk? Is there any possibility?

library(survminer)
library(survival)
library(dplyr)

lung <- lung %>% 
  mutate(timeyr = time / 365.25)

fit <- survfit(Surv(timeyr, status) ~ sex, data = lung)

ggsurv <- ggsurvplot(fit,  size = 1,  
                     xlab = "Follow-up time (years)",
                     ylab = "Survival probability (%)",
                     legend.labs = c("Drug 1234234", "Drug 25324"), 
                     linetype = "strata", 
                     xlim = c(0,2),
                     break.time.by = 1, 
                     palette = c("#E7B800", "#2E9FDF"), 
                     conf.int = FALSE, 
                     pval = FALSE, 
                     censor = FALSE,
                     risk.table = TRUE,
                     risk.table.title = "No. at risk",
                     risk.table.height = 0.2,
                     fontsize = 6,
                     tables.theme = theme_cleantable()
)
ggsurv

enter image description here


Solution

  • Got a workaround.
    Just add the lines of code and play around until it fits your needs:

    library(patchwork)
    
    ggsurv$table <- ggsurv$table +
      theme(
        plot.title = element_text(margin = margin(l = 16, r = 0, t = 0), hjust = 0, vjust = 5), #Adjust "No. at risk"
        axis.text.y = ggtext::element_markdown(margin = margin(l = 20, r = -100, t = -30), hjust = 0)) #Adjust Strata labels
    

    Combine the plot and the table:

    combined_plot <- ggsurv$plot / ggsurv$table + plot_layout(heights = c(3,0.8))
    
    combined_plot
    

    Results in:
    enter image description here