| 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:
Report bugs at https://github.com/ABohynDOE/ggstackedsurv/issues
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 |
risktable_stats |
Character vector of statistics to show, one row per
element. Elements of |
stats_label |
Named vector or list of custom row labels. The names are
the elements of |
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 |
size, ... |
Arguments passed to |
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().
-
"n.risk": number of subjects at risk (in the initial state). -
"n.event": number of transitions out of the initial state in the interval since the previous time shown in the table. -
"cum.event": cumulative number of transitions out of the initial state. -
"n.censor": number of censored observations in the interval. -
"cum.censor": cumulative number of censored observations.
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
statuswas 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 |
theme |
A list of ggplot2 theme components added to the plot.
Defaults to |
base_state |
A single string giving the name of the initial
(no event yet) state. Defaults to |
Value
A ggplot object.
See Also
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 |
combine_plots |
Logical. If |
Value
A ggplot/patchwork object, or a list of ggplots when
combine_plots = FALSE.
See Also
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.