# Load required libraries library(ggplot2) library(ggpubr) library(survminer) library(survival) # Load data data_1 <- read.csv(file = "XXX.csv", header = TRUE, sep = ",") # Define variables, titles, and thresholds predictors <- c("SII1424.8", "NLR2.8", "MLR0.2", "PLR184.4") titles <- c("SII", "NLR", "MLR", "PLR") thresholds <- c(1424.8, 2.8, 0.2, 184.4) # Create a list to store survival curve plots plots <- list() # Iterate through each variable, fit survival curves, and create plots for (i in 1:4) { predictor <- predictors[i] title <- titles[i] threshold <- thresholds[i] # Fit survival curve fit <- survfit(as.formula(paste("Surv(OSM, CENSOR) ~", predictor)), data = data_1) # Calculate sample sizes for legend labels n0 <- sum(data_1[[predictor]] == 0) n1 <- sum(data_1[[predictor]] == 1) legend.labs <- c(paste0(title, " ≤ ", threshold, " (n=", n0, ")"), paste0(title, " > ", threshold, " (n=", n1, ")")) # Create survival curve plot p <- ggsurvplot(fit, data = data_1, title = title, # Set title (SII, NLR, MLR, PLR) pval = TRUE, pval.method = FALSE, # Display p-value, hide method legend.title = paste0(title, " Level"), # Legend title legend.labs = legend.labs, # Legend labels, including sample sizes palette = c("dodgerblue3", "firebrick2"), # Color scheme risk.table = TRUE, # Include risk table cumevents = TRUE, # Include cumulative events table tables.height = 0.15, # Table height tables.theme = theme_cleantable(),# Clean table theme tables.y.text = FALSE, # Remove y-axis text in tables xlab = "Survival (Months)", # x-axis label break.time.by = 15) # x-axis time interval # Store the plot in the list plots[[i]] <- p } # Arrange the four plots into a 2x2 grid arrange_ggsurvplots(plots, print = TRUE, ncol = 2, nrow = 2)