Home > Software engineering >  Kaplan Meier curve (with ggsurvplot) with transparent background
Kaplan Meier curve (with ggsurvplot) with transparent background

Time:01-12

I made a Kaplan Meier curve with ggsurvplot as such:

kaplan_plot365d <- ggsurvplot(kmcurve365d,
                              conf.int = TRUE,
                         legend.labs = c('Trop-T normal and Trop-I normal', 'Trop-T elevated and Trop-I normal', "Trop-T elevated and Trop-I elevated"),
                         legend.title = '',
                         title = 'Kaplan-Meier Curve for 365-Day Mortality',
                         xlab = 'Time in Days', 
                         ylab = 'Survival probability', main = 'Kaplan-Meier Model',
                         ylim=c(min(0.7), max(1)),
                         xlim=c(min(0), max(366)),
                         break.time.by = 120,
                         palette = c('#FDE725FF', "#21908CFF", "#440154FF"),
                         risk.table = TRUE,
                         risk.table.y.text = TRUE, fontsize = 4, tables.theme = clean_theme(),
                         risk.table.title = "Survivors per group")  
guides(colour = guide_legend(nrow = 2))

kaplan_plot365d

I'd like to make the whole background transparent and then save it with a high quality to use it for a poster.

I tried to add those lines in the ggsurvplot() function: but it's not working:

    panel.background = element_rect(fill='transparent'), #transparent panel bg
    plot.background = element_rect(fill='transparent', color=NA), #transparent plot bg
    panel.grid.major = element_blank(), #remove major gridlines
    panel.grid.minor = element_blank(), #remove minor gridlines
    legend.background = element_rect(fill='transparent'), #transparent legend bg
    legend.box.background = element_rect(fill='transparent') #transparent legend panel
  

I then use this code to save it:

grid.draw.ggsurvplot <- function(x){
  survminer:::print.ggsurvplot(x, newpage = FALSE)}

ggsave("Kaplan-Meier_365d_withCI_Troponine.jpg",
         path = ("I:\\Personal\\Poster/"),
         plot = kaplan_plot365d,
         dpi = 300)

Can someone help me to make the background transparent?

Thank you!

CodePudding user response:

I think I see how it's working, and the intricacies of making sure it all comes together with ggsave, grid.draw and ggsurvplot. The key thing is that those lines go within ggtheme = theme(...) call in the ggsurvplot function:

library(survival)
library(survminer)

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

surv_plot_out <- ggsurvplot(fit, data = lung,
           legend.title = "Sex",
           legend.labs = c("Male", "Female"),
           conf.int = TRUE,
           risk.table = TRUE,
           tables.height = 0.2,
           tables.theme = theme_cleantable(),
           palette = c("#E7B800", "#2E9FDF"),
           ggtheme = theme(
             panel.background = element_rect(fill='transparent'), #transparent panel bg
             plot.background = element_rect(fill='transparent', color=NA), #transparent plot bg
             panel.grid.major = element_blank(), #remove major gridlines
             panel.grid.minor = element_blank(), #remove minor gridlines
             legend.background = element_rect(fill='transparent'), #transparent legend bg
             legend.box.background = element_rect(fill='transparent') #transparent legend panel
           ) # Change ggplot2 theme
) 

grid.draw.ggsurvplot <- function(x) survminer:::print.ggsurvplot(x, newpage = FALSE)

Then save as a png to give a transparent image:

ggsave("test_survplot.png", surv_plot_out, bg = "transparent")  
#> Saving 7 x 5 in image

The result:

  • Related