Package {ggstackedsurv}


Title: Stacked Probability Plots for Multi-State Survival Models
Version: 0.1.0
Description: Facilitate the creation of stacked probability plots for multi-state survival models with 'ggplot2'. Each function from the package creates a proper 'ggplot2' object that can be modified with any function from the 'ggplot2' or any of its extensions.
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.3.2
Depends: R (≥ 3.5)
LazyData: true
Imports: cli, ggplot2, glue, grid, patchwork
Suggests: survival, testthat (≥ 3.0.0), withr
Config/testthat/edition: 3
URL: https://github.com/ABohynDOE/ggstackedsurv
BugReports: https://github.com/ABohynDOE/ggstackedsurv/issues
NeedsCompilation: no
Packaged: 2026-11-08 11:34:15 UTC; u0133728
Author: Alexandre Bohyn [aut, cre, cph]
Maintainer: Alexandre Bohyn <alexandre.bohyn@kuleuven.be>
Repository: CRAN
Date/Publication: 2026-11-29 11:00:02 UTC

ggstackedsurv: Stacked Probability Plots for Multi-State Survival Models

Description

Facilitate the creation of stacked probability plots for multi-state survival models with 'ggplot2'. Each function from the package creates a proper 'ggplot2' object that can be modified with any function from the 'ggplot2' or any of its extensions.

Author(s)

Maintainer: Alexandre Bohyn alexandre.bohyn@kuleuven.be [copyright holder]

See Also

Useful links:


Add a risk table

Description

Add a risk table below a ggstackedsurv() plot showing, at chosen times, the number at risk, the number of events and the number of censored observations.

Usage

add_risktable(
  times = NULL,
  risktable_stats = c("n.risk", "cum.event"),
  stats_label = NULL,
  risktable_height = NULL,
  theme = theme_risktable_default(),
  size = 3.5,
  ...
)

Arguments

times

Numeric vector of times where the risk table values are placed. Default are the breaks shown on the x-axis. This does not change the tick marks of the figure, use ggplot2::scale_x_continuous(breaks =) for that.

risktable_stats

Character vector of statistics to show, one row per element. Elements of c("n.risk", "n.event", "cum.event", "n.censor", "cum.censor") or glue strings combining them. Default is c("n.risk", "cum.event"). See the "What the statistics mean" section for details.

stats_label

Named vector or list of custom row labels. The names are the elements of risktable_stats and the values the labels. An unnamed vector of the same length as risktable_stats is used in order.

risktable_height

A number between 0 and 1: the proportion of the final figure occupied by the risk table. Default is chosen from the number of rows.

theme

A risk table theme. Default is theme_risktable_default().

size, ...

Arguments passed to ggplot2::geom_text(), for example size = 4 to enlarge the numbers.

Value

An object that can be added to a ggstackedsurv() plot with +.

What the statistics mean

The statistics describe the subjects who have not yet left the initial state of the multi-state model, i.e. the base_state of ggstackedsurv().

Customize statistics

risktable_stats accepts glue-like syntax. For example risktable_stats = "{n.risk} ({cum.event})" puts the number at risk and the number of events in the same row.

See Also

ggstackedsurv(), ggstackedsurv_build()

Examples


library(survival)

fit <- survfit(Surv(time, status) ~ 1, data = ecmo)
p <- ggstackedsurv(fit)

p + add_risktable()

p + add_risktable(times = c(10, 20, 40, 60))

p + add_risktable(
  risktable_stats = c("n.risk"),
  stats_label = c(n.risk = "Number at risk")
)


ECMO patient outcomes

Description

A fake dataset of 83 patients who received extracorporeal membrane oxygenation (ECMO). Each row gives a status and the time (in days from ECMO onset) at which that status was recorded. It serves as an example for a competing risks analysis, where the event of interest is death on ECMO, and where the competing events are discharge from the ICU and ECMO discontinuation. The dataset is in long format, with one row per patient per status.

Usage

ecmo

Format

A data frame with 186 rows and 3 variables:

id

Patient identifier, a factor with 83 levels.

time

Integer. Days from ECMO onset at which status was recorded. Ranges from 1 to 90.

status

Factor with levels "censored", "Died", "Alive and out of ICU", and "In ICU and weaned off ECMO", in that order. "censored" marks the end of follow-up without a death.

Source

The dataset was generated by hand, trying to mimic the patients presented in the paper by Schmidt, M., et al. (2020). Extracorporeal membrane oxygenation for severe acute respiratory distress syndrome associated with COVID-19: a retrospective cohort study. The Lancet Respiratory Medicine, 8(11), 1121-1131.

Examples

head(ecmo)
table(ecmo$status)

Stacked survival plot for a multi-state survfit object

Description

Draws the state occupancy probabilities from a multi-state survfit object as a stacked step-area plot. Add a risk table with add_risktable().

Usage

ggstackedsurv(
  x,
  theme = theme_ggstackedsurv_default(),
  base_state = "Censored"
)

Arguments

x

A multi-state survfit object, such as one from survival::survfit() with a factor status in Surv(time, status).

theme

A list of ggplot2 theme components added to the plot. Defaults to theme_ggstackedsurv_default().

base_state

A single string giving the name of the initial (no event yet) state. Defaults to "Censored".

Value

A ggplot object.

See Also

add_risktable()

Examples


library(survival)
# Fit a multi-state model with the "ecmo" dataset
fit <- survfit(Surv(time, status) ~ 1, data = ecmo)
# Generate a stacked probability plot
ggstackedsurv(fit, base_state = "On ECMO")


Build a ggstackedsurv plot

Description

Combines a ggstackedsurv() plot with the risk table added by add_risktable(), if any. This is what happens when a ggstackedsurv plot is printed or saved with ggplot2::ggsave(). Call it directly to get the pieces and arrange them yourself.

Usage

ggstackedsurv_build(x, combine_plots = TRUE)

Arguments

x

A plot created by ggstackedsurv().

combine_plots

Logical. If TRUE (default), the plot and the risk table are stacked into one patchwork figure. If FALSE, a list with elements plot and table is returned (table is NULL when there is no risk table).

Value

A ggplot/patchwork object, or a list of ggplots when combine_plots = FALSE.

See Also

add_risktable()

Examples


library(survival)
fit <- survfit(Surv(time, status) ~ 1, data = ecmo)
p <- ggstackedsurv(fit) + add_risktable()
parts <- ggstackedsurv_build(p, combine_plots = FALSE)
names(parts)


Default theme of the risk table

Description

A list of ggplot2 theme components used by add_risktable(). Pass your own list of components to add_risktable(theme = ) to change the look.

Usage

theme_risktable_default(axis.text.y.size = 10, plot.title.size = 10.75)

Arguments

axis.text.y.size

Size of the row labels.

plot.title.size

Size of the title.

Value

A list of ggplot2 theme components.

See Also

add_risktable()