Kaplan-Meier Curves

The first step is to use the survival (run analysis) and survminer (create graphs) packages to run a survival analysis and create a Kaplan-Meier curve and compare differences between groups using the log-rank test. The “cancer” dataset is a simulated dataset that contains information on survival times in patients with Lung cancer. We will do it all in one step using pipes to practice our dplyr.

Manipulations required to the data prior to running to model include

Code
library(survival)
library(survminer)
library(tidyverse)
library(jtools)
library(ggsci)


cancer %>% 
  mutate( 
    outcome = case_when( # Recoding status
      status == 1 ~ 0,
      status == 2 ~ 1
    ),
    sex = case_when( # Recoding sex
      sex == 1 ~ "Male",
      sex == 2 ~ "Female"
    )) %>% 
  rename(
    time_at_risk = time
  ) %>% 
  ggsurvplot( # Plotting function
    survfit(Surv(time_at_risk, outcome) ~ sex, data = .), # Model formula
    data = ., # Required to tell the function that we are piping the model to it
    risk.table = TRUE, # Add number at risk table
    cumevents = TRUE, # Add a cumulative events table
    fontsize = 3, # Reducing size of text in the tables
    tables.height = .26, # Reducing the height of the tables
    pval = TRUE, # Add log-rank test 
    pval.size = 3.5, # Change size of p-value text
    size = 0.4, # Width of the lines
    censor = F, # Remove the default markers where a censoring event has occurred
    conf.int = TRUE, # Add confidence intervals
    conf.int.alpha=c(0.15), # Reducing the colour intensity of the confidence intervals
    ggtheme = jtools::theme_nice(), # Adding a nice theme
    title = "Figure 1. Survival stratified by sex", # Add a title
    font.title = "bold", # Make said title bold
    xlab = "Time from study entry (months)", # X-axis label
    xlim = c(0, 750), # Set limits on the X-axis
    xscale = "d_m", # Changing the X-axis scale from days to months
    break.time.by = 365.25/2, # Setting the breaks on the X-axis to 6-months
    legend.title="", # Remove the heading above the legend
    legend.labs = c("Female", "Male"), # Changing the labels on the legend
    axes.offset = FALSE, # So graph starts at X-0, Y-0
    palette = "nejm",
    tables.theme = theme_cleantable(), # clean theme for tables
    )