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
Creating a new outcome variable from the “status” variable with 1s and 0s instead of 2s and 1s (everyone prefers 1s and 0s)
Renaming the levels of the “sex” variable to Male and Female instead of 1 and 2.
Renaming time to “time_at_risk” to make it more explicit.
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 functionsurvfit(Surv(time_at_risk, outcome) ~ sex, data = .), # Model formuladata = ., # Required to tell the function that we are piping the model to itrisk.table =TRUE, # Add number at risk tablecumevents =TRUE, # Add a cumulative events tablefontsize =3, # Reducing size of text in the tablestables.height = .26, # Reducing the height of the tablespval =TRUE, # Add log-rank test pval.size =3.5, # Change size of p-value textsize =0.4, # Width of the linescensor = F, # Remove the default markers where a censoring event has occurredconf.int =TRUE, # Add confidence intervalsconf.int.alpha=c(0.15), # Reducing the colour intensity of the confidence intervalsggtheme = jtools::theme_nice(), # Adding a nice themetitle ="Figure 1. Survival stratified by sex", # Add a titlefont.title ="bold", # Make said title boldxlab ="Time from study entry (months)", # X-axis labelxlim =c(0, 750), # Set limits on the X-axisxscale ="d_m", # Changing the X-axis scale from days to monthsbreak.time.by =365.25/2, # Setting the breaks on the X-axis to 6-monthslegend.title="", # Remove the heading above the legendlegend.labs =c("Female", "Male"), # Changing the labels on the legendaxes.offset =FALSE, # So graph starts at X-0, Y-0palette ="nejm",tables.theme =theme_cleantable(), # clean theme for tables )