📜 Simple Survival Plots in R

April 9, 2018  

This is a simple example of a survival plot. You can do quite a few interesting things with Survival plots - for instance, in a lot of human factors simulation research, you can examine time to event detection or system failure.

library(survminer)
library(survival)
library(ggplot2)

# Load up a dataset
survdats <- structure(list(survival = c(180.61, 154.55, 250.86, 144.53, 202.66, 
                                        318.49, 58.26, 14.35, 85.75, 43.36, 162.1, 200.6, 21.75, 485.57, 
                                        164.3, 164.3, 247.51, 96.12, 364.96, 155.4, 117.97, 34.96, 14.02, 
                                        179.19, 164.22, 114, 294.75, 304.5)), .Names = "survival", class = "data.frame", row.names = c(NA, 
                                                                                                                                       -28L))

# Perform a survfit on a Surv object
sfit <- (survfit(Surv(survdats$survival)~1))

ggsurvplot(sfit, data = survdats)


# Customized survival curves
ggsurvplot(fit = sfit, data = survdats,
           surv.median.line = "hv", # Add medians survival
           
           # Change legends: title & labels
           title="Kaplan-Meier Curve for Automation Failure Detection",
           ylab = "Survival Probability",
           # Add p-value and tervals

           conf.int = TRUE,
           # Add risk table
           risk.table = TRUE,
           tables.height = 0.2,
           tables.theme = theme_cleantable(),
           
           # Color palettes. Use custom color: c("#E7B800", "#2E9FDF"),
           # or brewer color (e.g.: "Dark2"), or ggsci color (e.g.: "jco")
           palette = c("#E7B800", "#2E9FDF"),
           ggtheme = theme_bw() # Change ggplot2 theme
)