Learn R Programming

ggsurvfit

Introduction

The ggsurvfit package eases the creation of time-to-event (aka survival) summary figures with ggplot2. The concise and modular code creates images that are ready for publication or sharing. Competing risks cumulative incidence is also supported via ggcuminc().

Why ggsurvfit?

  • Use ggplot2 functions: Each ggsurvfit add-on function (e.g. add_confidence_interval(), add_risktable(), etc.) is written as a proper ggplot2 ‘geom’, meaning the package functions can be woven with ggplot2 functions seamlessly. You don’t need to learn how to style the plot within the ggsurvfit functions: rather, rely on the suite of ggplot2 functions you already know.

  • Publishable Legends: Raw variable names do not appear in the figure legend, e.g. "sex=Female".

  • Limitless Customization: You can modify the x-axis scales or any other plot feature and the risk table will still align with the plot.

  • Simple Saving: Save individual images easily with ggplot2::ggsave().

Installation

Install ggsurvfit from CRAN with:

install.packages("ggsurvfit")

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("pharmaverse/ggsurvfit")

Examples

Review the figure gallery for many more examples.

The code below constructs a basic {ggsurvfit} figure without customization.

library(ggsurvfit)
#> Loading required package: ggplot2

p <- survfit2(Surv(time, status) ~ surg, data = df_colon) |>
  ggsurvfit(linewidth = 1) +
  add_confidence_interval() +
  add_risktable() +
  add_quantile(y_value = 0.6, color = "gray50", linewidth = 0.75) +
  scale_ggsurvfit()

Any figure created with {ggsurvfit} can be customized using {ggplot2} functions.

p +
  # limit plot to show 8 years and less
  coord_cartesian(xlim = c(0, 8)) +
  # update figure labels/titles
  labs(
    y = "Percentage Survival",
    title = "Recurrence by Time From Surgery to Randomization",
  )

survfit2() vs survfit()

Both functions have identical inputs, so why do we need survfit2()? The survfit2() tracks the environment from which the function was called, resulting in the following benefits.

  • We can reliably remove the raw variable names from the figure legend, e.g. SEX=Female.
  • P-values can be calculated with survfit_p() and added to figures.
  • The items above are often possible using survfit(). However, by utilizing the calling environment we are assured the correct elements are found, rather than crossing our fingers that the search path contains the needed elements.

CDISC ADaM ADTTE

The package also includes gems for those using the CDISC ADaM ADTTE v1.0 data model.

If columns "PARAM" or "PARAMCD" are present in the data frame passed to survfit2(), their values will be used to construct default labels in the ggsurvfit() figure.

The event indicator in ADTTE data sets is named "CNSR" and is coded in the opposite way the survival package expects outcomes—1 = 'censored' and 0 = 'event'. This difference creates an opportunity for errors to be introduced in an analysis. The ggsurvfit package exports a function called Surv_CNSR() to resolve this concern. The function creates a survival object (e.g. survival::Surv()) that uses CDISC ADaM ADTTE coding conventions as the default values. The function can be used in ggsurvfit as well as any other package that uses survival::Surv().

survfit(Surv_CNSR() ~ 1, adtte)
#> Call: survfit(formula = Surv_CNSR() ~ 1, data = adtte)
#> 
#>         n events median 0.95LCL 0.95UCL
#> [1,] 2199    755    3.2     3.1    3.56

Related Packages

Copy Link

Version

Install

install.packages('ggsurvfit')

Monthly Downloads

5,878

Version

1.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Daniel D. Sjoberg

Last Published

May 8th, 2024

Functions in ggsurvfit (1.1.0)

reexports

Objects exported from other packages
scale_ggsurvfit

Apply Scales
survfit2_p

Calculate p-value
theme_ggsurvfit

Survfit Plot Themes
theme_risktable

Risk Table Themes
tidy_cuminc

Tidy a cuminc object
grid.draw_ggsurvfit

Draw ggsurvfit object
print_ggsurvfit

Print ggsurvfit object
survfit2

Create survival curves
stepribbon

Step ribbon statistic
tidy_survfit

Tidy a survfit object
format_p

Format p-value
add_risktable

Add risk table
adtte

Example phase III clinical trial data set
Surv_CNSR

Create a Survival Outcome from CDISC Data
add_confidence_interval

Add Confidence Interval
add_risktable_strata_symbol

Use Symbol for Strata in Risk Table
add_censor_mark

Add Censor Marking
add_legend_title

Add Legend Title
add_quantile

Add Quantile Annotation
add_pvalue

Add p-value
df_colon

Formatted Copy of survival::colon
df_lung

Formatted Copy of survival::lung
ggsurvfit_build

Build 'ggsurvfit' Object
ggsurvfit_options

Global Options
ggsurvfit

Plot Survival Probability
ggsurvfit_align_plots

Align Plots
ggcuminc

Plot Cumulative Incidence
ggsurvfit-package

ggsurvfit: Flexible Time-to-Event Figures