Introduction

The km_plot function in this packages generates Kaplain-Meier survival curves and corresponding risk table. An exhaustive list of options included in this tutorial can be found with ?km_plot following package installation.  

 

Installing the ‘kmrskplot’ package

Install the package from GitHub using Hadley’s devtools package.

library(devtools)
install_github("emwozniak/kmrskplot")
library(kmrskplot)

 

Building a plot

The examples in this tutorial use the Mayo Clinic Primary Biliary Cirrhosis Data found in the survival package.

Minimal required inputs

The only required input to the km_plot function is a survfit fitted model object. By default, a KM curve is generated, but the complement may be plotted instead with the type = "1-km" option.

Example

library(survival)
# Set up data as a survival object
# Censor those who receive a transplant or are indicated by the dataset as censored
pbc_km <- na.omit(pbc[, c("time", "status", "trt")])
pbc_km$status[pbc_km$status == 1] <- 0
pbc_km$status[pbc_km$status == 2] <- 1
pbc_km$SurvObj <- with(pbc_km,
                       Surv(time  = time,
                            event = status)
                       )
fit <- survfit(SurvObj ~ trt,
               data = pbc_km,
               conf.type = "log-log")

# KM plot for time to death using all default options
km_plot(fit = fit)

Customized plot with some common options

Some typical options for customizing plots include choice of color, labels, and major x-axis gridlines for risk table calculation.

Example

# Customized pretty plot with some usual options
km_plot(fit  = fit,
        type = "km",
        col  = c("orange3", "olivedrab"),
        # Add an extra major gridline and risk table column at time 4500
        xlim_major       = c(0, 1000, 2000, 3000, 4000, 4500, 5000),
        plot_title       = "Primary biliary cirrhosis survival",
        plot_subtitle    = "By randomized treatment status",
        y_axis_label     = "Survival",
        x_axis_label     = "Time since registration (days)",
        group_names      = c("D-penacillamine", "Placebo"),
        risk_table_title = bquote(italic("Patients at risk:")))

Customized plot with many options

Other options include changing the font family, gridline colors, order of groups in the risk table, and title size. Additionally, extra left margin space can be added, which is useful in particular to accomodate long risk table titles.

Example

km_plot(fit               = fit,
        font_family       = "mono",
        type              = "1-km",
        col               = c("black", "black"),
        lty               = c(1, 2),
        lwd               = c(2, 2),
        xlim_major        = c(0, 500, 1000, 2000, 3000, 3375, 4000, 4500, 5000),
        col_grid_major    = "gray80",
        col_grid_minor    = "gray92",
        plot_title        = bquote(bold("Death following transplant registration")),
        plot_subtitle     = "Mayo Clinic 1974-1984",
        plot_title_size   = 1,
        y_axis_label      = bquote(bold("Cumulative incidence")),
        x_axis_label      = bquote(bold("Days")),
        group_names       = c("D-penacillamine", "Placebo"),
        group_order       = c(2, 1),
        risk_table_title  = bquote(bold(underline("RISK TABLE"))),
        extra_left_margin = 5)