Search code examples
rggplot2survival-analysissurvival

R survminer::ggsurvplot distance between axis labels and axis title


Reproducible example:

require(survival)
require(survminer)
require(ggplot2)

set.seed(42)

a <- c(32,291,545, 44,3,792,352, 20,615,169, 61,156, 88,863,255, 33,132,5,
       63,8,964,831, 55,133, 12, 54,261,867, 17, 12,699,233,251,446, 43,223, 
       374, 75, 34, 69,120, 84,134,8,806,8, 48, 86,211, 2436, 61, 81, 87, 12, 
       838, 34, 17,141, 44,155,128,6, 29, 16, 16, 34,130,430,325, 41, 28, 53,
       32,291,545, 44,3,792,352, 20,615,169, 61,156, 88,863,255, 33,132,5,
       63,8,964,831, 55,133, 12, 54,261,867, 17, 12,699,233,251,446, 43,223,
       86, 54,3,630, 93,699, 25,746,6, 46, 22, 60,395,402,151, 26, 38,125, 
       75, 34, 69,120, 84,134,8,806,8, 48, 86,211, 2436, 61,
       191, 49, 59,6, 34, 56,2, 96, 422, 45, 70)

b <- sample(c("Alpha", "Beta", "Gamma"), length(a), replace = T)
c <- sample(0:1, length(a), replace = T)
df <- data.frame(a, b, c)

df$b <- factor(df$b, levels = c("Alpha", "Beta", "Gamma"))
avg <- survfit(Surv(time = a, event = c) ~ 1, data = df)
surv_b <- survfit(Surv(time = a, event = c) ~ b, data = df)
surv_comb <- list(surv_b, avg)

survminer::ggsurvplot_combine(
  surv_comb, 
  data = df,
  combine = TRUE,
  censor = FALSE,
  legend = "none",
  palette = c("orange", "steelblue", "seagreen", "black"),
  linetype = c(rep("solid",3), "dashed"),
  )

This produces the chart below: enter image description here

with the axis titles ("Survival probability") squashed up against the axis labels. I have tried a number of ways to increase the gap, including hjust/vjust and also things like axis.title.x = element_text(margin = margin(t = 10, "mm")) in various places, but nothing seems to have any effect. If I try to assign the plot to an object and then add + theme(), I get the error that ggsurvplot does not support this. Googling only returns answers regarding ggplot, not ggsurvplot. Does anyone know how to give my axis titles a bit of breathing space, please? Thank you!


Solution

  • A ggsurvplot object is a list where the plot is stored as an element named plot. Hence, to apply a theme adjustment you have to "add" it to the the plot element:

    library(survminer)
    library(ggplot2)
    
    p <- survminer::ggsurvplot_combine(
      surv_comb, 
      data = df,
      combine = TRUE,
      censor = FALSE,
      legend = "none",
      palette = c("orange", "steelblue", "seagreen", "black"),
      linetype = c(rep("solid",3), "dashed"),
      theme = theme_survminer()
    )
    
    p$plot <- p$plot +
      theme(axis.title.x = element_text(margin = margin(t = 50)))
    
    p
    

    enter image description here