Search code examples
rggplot2themessurvivalsurvminer

Applying theme_tq removes subgroup colors from my table (survminer)


I am trying to make Kaplan Meier curves using the survminer package and then using ggplot2 to polish how it all looks. After I have created my survival object(g2) using the ggsurvplot function, I then want to create a table showing survival data. This all works very well until I apply theme_tq to the table. This is the code I am using:

I have added two images showing the before and after applying theme_tq, as you can see, the colors for each subgroup disappear, it all becomes black.Before After

g2_table <- g2$table +
  scale_color_tq() +
  scale_fill_tq() +
  theme_tq(base_size = 20, base_family = "paper_II_font") +
  theme(plot.margin = unit(c(0,0.2,0.01,0.1), "cm"))+
  theme(axis.text.x= element_text(face = "bold")) +
  theme(axis.text.y= element_text(face = "bold")) +
  theme(axis.title.y = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.text.x=element_blank()) +
  theme(panel.grid = element_blank())

I was expecting it to create the table with theme_tq however I also expected the colors to be unchanged, I don't understand why they changed to all black.


Solution

  • The issue is that by applying theme_tq this way you are overwriting the theme already applied by ggsurvplot under the hood, including the theme adjustments to apply the colors to the axis.text. Instead I would suggest to apply your desired theme including any tweaks directly by passing it to the table.theme (and/or ggtheme) argument of ggsurvplot. Also, as far as I get it, scale_color_tq and scale_fill_tq will have no effect. Instead pass your desired colors to the palette= argument, e.g. for the default color palette used by scale_color_tq use palette_light().

    Using the default example from ?ggsurvplot:

    library(survminer)
    library(survival)
    library(tidyquant)
    
    fit <- survfit(Surv(time, status) ~ sex, data = lung)
    
    table_theme <- theme_tq(
      base_size = 20,
      base_family = "paper_II_font"
    ) +
      theme(
        plot.margin = unit(c(0, 0.2, 0.01, 0.1), "cm"),
        axis.text = element_text(face = "bold"),
        axis.title = element_blank(),
        axis.text.x = element_blank(),
        panel.grid = element_blank()
      )
    
    # Basic survival curves
    p <- ggsurvplot(fit,
      data = lung, risk.table = TRUE,
      main = "Survival curve",
      submain = "Based on Kaplan-Meier estimates",
      caption = "created with survminer",
      tables.theme = table_theme,
      palette = unname(palette_light())
    )
    
    p$table <- p$table
    
    p
    

    enter image description here