| Title: | 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. |
| Authors: | Alexandre Bohyn [aut, cre, cph] |
| Maintainer: | Alexandre Bohyn <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-11-30 15:35:31 UTC |
| Source: | https://github.com/abohyndoe/ggstackedsurv |
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.
add_risktable( times = NULL, risktable_stats = c("n.risk", "cum.event"), stats_label = NULL, risktable_height = NULL, theme = theme_risktable_default(), size = 3.5, ... )add_risktable( times = NULL, risktable_stats = c("n.risk", "cum.event"), stats_label = NULL, risktable_height = NULL, theme = theme_risktable_default(), size = 3.5, ... )
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 |
An object that can be added to a ggstackedsurv() plot with +.
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.
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.
ggstackedsurv(), ggstackedsurv_build()
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") )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") )
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.
ecmoecmo
A data frame with 186 rows and 3 variables:
Patient identifier, a factor with 83 levels.
Integer. Days from ECMO onset at which status was recorded. Ranges from 1 to 90.
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.
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.
head(ecmo) table(ecmo$status)head(ecmo) table(ecmo$status)
Draws the state occupancy probabilities from a multi-state survfit object
as a stacked step-area plot. Add a risk table with add_risktable().
ggstackedsurv( x, theme = theme_ggstackedsurv_default(), base_state = "Censored" )ggstackedsurv( x, theme = theme_ggstackedsurv_default(), base_state = "Censored" )
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 |
A ggplot object.
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")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")
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.
ggstackedsurv_build(x, combine_plots = TRUE)ggstackedsurv_build(x, combine_plots = TRUE)
x |
A plot created by |
combine_plots |
Logical. If |
A ggplot/patchwork object, or a list of ggplots when
combine_plots = FALSE.
library(survival) fit <- survfit(Surv(time, status) ~ 1, data = ecmo) p <- ggstackedsurv(fit) + add_risktable() parts <- ggstackedsurv_build(p, combine_plots = FALSE) names(parts)library(survival) fit <- survfit(Surv(time, status) ~ 1, data = ecmo) p <- ggstackedsurv(fit) + add_risktable() parts <- ggstackedsurv_build(p, combine_plots = FALSE) names(parts)
A list of ggplot2 theme components used by add_risktable(). Pass your own
list of components to add_risktable(theme = ) to change the look.
theme_risktable_default(axis.text.y.size = 10, plot.title.size = 10.75)theme_risktable_default(axis.text.y.size = 10, plot.title.size = 10.75)
axis.text.y.size |
Size of the row labels. |
plot.title.size |
Size of the title. |
A list of ggplot2 theme components.