Search code examples
rggplot2survival-analysissurvminer

I want to annotate (add letters eg A and B) to the resulting 2 Kaplan Meier curves plots using splots and survminer in R


I need to add letters eg A and B to the resulting 2 Kaplan Meier curves plots

code

library(survminer)
# Fit survival curves
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# List of ggsurvplots
require("survminer")
splots <- list()
splots[[1]] <- ggsurvplot(fit, data = lung, risk.table = TRUE, ggtheme = theme_minimal())
splots[[2]] <- ggsurvplot(fit, data = lung, risk.table = TRUE, ggtheme = theme_grey())

# Arrange multiple ggsurvplots and print the output
arrange_ggsurvplots(splots, print = TRUE,
  ncol = 2, nrow = 1, risk.table.height = 0.4)

# Arrange and save into pdf file
res <- arrange_ggsurvplots(splots, print = FALSE)
ggsave("myfile.pdf", res, width = 15, height = 15, units = "cm")

So I need the resulting plot like this

enter image description here


Solution

  • Calling ggsurvplot(...,risk.table=TRUE,...) creates a list that holds a plot and a table. You can access the plot with splots[[...]]$plot and then add a figure label with labs(tag="A").

    library(survminer)
    # Fit survival curves
    require("survival")
    fit<- survfit(Surv(time, status) ~ sex, data = lung)
    # List of ggsurvplots
    splots <- list()
    splots[[1]] <- ggsurvplot(fit, data = lung, risk.table = TRUE, ggtheme = theme_minimal()  ) 
    splots[[2]] <- ggsurvplot(fit, data = lung, risk.table = TRUE, ggtheme = theme_grey()) 
    
    # access the plot objects and add a tag with labs()
    splots[[1]]$plot<-splots[[1]]$plot + labs(tag="A")
    splots[[2]]$plot<-splots[[2]]$plot + labs(tag="B")
    
    # Arrange multiple ggsurvplots and print the output
    arrange_ggsurvplots(splots, print = TRUE,
                        ncol = 2, nrow = 1, risk.table.height = 0.4)
    
    # Arrange and save into pdf file
    res <- arrange_ggsurvplots(splots, print = FALSE)
    ggsave("myfile.pdf", res, width = 15, height = 15, units = "cm")
    

    enter image description here