Using Survminer Package and ggplot2

Loading Libraries

# Loading necessary libraries
library(ggplot2)
library(survival)
library(survminer)
library(readxl)

Loading Data

# Loading the data
data <- read_excel("D:/Part_time_CECAREUS/Trial graphs/Data/Data for KM Curve.xlsx", sheet = "KM Curve")
# Ensure the 'Group' variable categorizes each individual into one of three groups
data$Group <- as.factor(data$Class)  # Convert to factor if not already

Data Preparation

# Create a Surv object
surv_object <- Surv(time = data$`Survival Time (Months)`, event = data$`Status (Alive)`)
# Fit the Kaplan-Meier survival model using the Group variable
km_fit <- survfit(surv_object ~ Group, data = data)

Plotting the chart

# Plot the Kaplan-Meier survival curve
survplot <- ggsurvplot(km_fit,
                       data = data,
                       pval = TRUE,  # Display the p-value of the log-rank test
                       conf.int = FALSE,  # Show confidence intervals
                       palette = c( "#e7298a", "#1b9e77", '#e6ab02'),  # Assign colors to each group
                       xlab = "Time (Months)",  # Customize X-axis label
                       ylab = "Survival Probability",  # Customize Y-axis label
                       title = "Kaplan-Meier Survival Curve",  # Customize plot title
                       legend.title = "Treatment Group",  # Customize legend title
                       legend.labs = c("Resected", "Unresected", "Overall"),  # Labels for each group
                       surv.median.line = "none",  # Add median survival line
                       break.time.by = 10,
                       risk.table = FALSE,
                       risk.table.title = "Number at risk by time",
                       risk.table.height = 0.3, 
                       censor = TRUE,
                       breaks.y = seq(0, 1, by = 0.25,)
)
#setting x and y axis limits
survplot$plot <- survplot$plot + 
  xlim(c(0, 180)) +  
  ylim(c(0.25, 1))
#adding theme
survplot$plot <- survplot$plot + my_theme()
print(survplot)
Kaplan-Meier Survival Curve

Kaplan-Meier Survival Curve

# Plot the Kaplan-Meier survival curve
survplot <- ggsurvplot(km_fit,
                       data = data,
                       pval = TRUE,  # Display the p-value of the log-rank test
                       conf.int = FALSE,  # Show confidence intervals
                       palette = c( "#e7298a", "#1b9e77", '#e6ab02'),  # Assign colors to each group
                       xlab = "Time (Months)",  # Customize X-axis label
                       ylab = "Survival Probability",  # Customize Y-axis label
                       title = "Kaplan-Meier Survival Curve",
                       legend.position = "right",# Customize plot title
                       legend.title = "Treatment Group",  # Customize legend title
                       legend.labs = c("Resected", "Unresected", "Overall"),  # Labels for each group
                       surv.median.line = "none",  # Add median survival line
                       break.time.by = 10,
                       risk.table = FALSE,
                       risk.table.title = "Number at risk by time",
                       risk.table.height = 0.3, 
                       censor = FALSE,
                       breaks.y = seq(0, 1, by = 0.25,)
)
#setting x and y axis limits
survplot$plot <- survplot$plot + 
  xlim(c(0, 180)) +  
  ylim(c(0.25, 1))
#adding theme
survplot$plot <- survplot$plot + my_theme()
print(survplot$plot)
Kaplan-Meier Survival Curve

Kaplan-Meier Survival Curve

# Plot the Kaplan-Meier survival curve
survplot <- ggsurvplot(km_fit,
                       data = data,
                       pval = TRUE,  # Display the p-value of the log-rank test
                       conf.int = FALSE,  # Show confidence intervals
                       palette = c( "#e7298a", "#1b9e77", '#e6ab02'),  # Assign colors to each group
                       xlab = "Time (Months)",  # Customize X-axis label
                       ylab = "Survival Probability",  # Customize Y-axis label
                       title = "Kaplan-Meier Survival Curve",
                       legend.position = "right",# Customize plot title
                       legend.title = "Treatment Group",  # Customize legend title
                       legend.labs = c("Resected", "Unresected", "Overall"),  # Labels for each group
                       surv.median.line = "none",  # Add median survival line
                       break.time.by = 10,
                       risk.table = FALSE,
                       risk.table.title = "Number at risk by time",
                       risk.table.height = 0.3, 
                       censor = FALSE,
                       legend.position = "top",
                       breaks.y = seq(0, 1, by = 0.25,)
)
#setting x and y axis limits
survplot$plot <- survplot$plot + 
  xlim(c(0, 180)) +  
  ylim(c(0, 1))
#adding theme
survplot$plot <- survplot$plot + my_theme()
print(survplot$plot)
Kaplan-Meier Survival Curve

Kaplan-Meier Survival Curve

Risk Table

# Create the risk table plot
risk_table_plot <- ggsurvplot(km_fit, data = data,
                              plot = FALSE,
                              risk.table = TRUE,
                              risk.table.title = "Number at risk by time",
                       risk.table.height = 0.3,
                       break.time.by = 20)
risk_table_plot$table <- risk_table_plot$table  + my_theme()
print(risk_table_plot$table)

Cumulative Hazard Charts (Survminer package)

Plotting the chart

#Cumulative hazard chart with survminer package
surv_cumulative <- ggsurvplot(km_fit,
           data = data,
           fun = 'event',
           cumevents = TRUE,
           censor = TRUE,
           conf.int = TRUE,
           xlim = c(0,180),
           risk.table = FALSE,
           break.time.by = 20,
           xlab = "Time (Months)",  # Customize X-axis label
           ylab = "Cumulative Hazard",  # Customize Y-axis label
           title = "Cumulative Hazard Curve",  # Customize plot title
           legend.title = "Treatment Group",  # Customize legend title
           legend.labs = c("Resected", "Unresected", "Overall"),
           palette = c( "#e7298a", "#1b9e77", '#e6ab02'),
           ggtheme = my_theme()
)
print(surv_cumulative)
Cumulative Hazard Curve

Cumulative Hazard Curve

Survminer charts can be modified to include interactivity using plotly package.

Using ggfortify Package

library(ggfortify)
autoplot(km_fit,
         conf.int = TRUE)+
  labs(x = "Survival Time (Months)", y = "Survival Probabilities",
       title = "Kaplan Meier Curve")+
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.x = element_text(face = "bold", colour = "#FF7A33", size = 12),
        axis.title.y = element_text(face = "bold", colour = "#FF7A33", size = 12),
        legend.title = element_text(face = "bold", size = 10))
Survival Curve

Survival Curve

Using Highcharter Package

Loading required libraries

library(highcharter)
library(tidyr)
library(dplyr) 

Data preparation and plotting the chart

hchart(km_fit, 
       ranges = TRUE) %>%
  hc_tooltip(headerFormat = "<b> Month {point.key} </b> <br>",
             borderWidth = 4) %>%
  hc_title(text = "Kaplan Meier Curve") %>%
  hc_xAxis(title = list(text = "Survival Time (Months)")) %>%
  hc_yAxis(title = list(text = "Survival Probability"), min = 0.25)