create_step_curve: Create a data.frame to plot step lines

View source: R/step_curves.R

create_step_curveR Documentation

Create a data.frame to plot step lines

Description

Creates survival probabilities from time and censoring information and generates a risk table that includes the survival probabilities and number at risk in addition to the data provided. This data.frame can be used to plot step line outcomes such as time-to-event (Kaplan-Meier curves) and magnitude breadth (MB) curves.

Usage

create_step_curve(x, event = NULL, flip_surv = FALSE, flip_top_x = Inf)

Arguments

x

Time values used to create the x-axis in step curves (numeric vector)

event

event status, 0=censor and 1=event (numeric vector). If NULL assumes no censoring

flip_surv

logical indicating if reverse survival estimates should be calculated. Default is FALSE.

flip_top_x

value to set x for top point for plotting. Only used if flip_surv = TRUE. Default is Inf.

Details

The output of survival probabilities can be used for plotting step function curves, with time on the x axis, surv on the y axis, and n.censor == 1 subset can be used for a ggplot2::geom_point() layer.

If flip_surv = TRUE there is an additional row at the bottom of the data.frame needed for horizontal line at the top of the plot

Value

Returns a data frame with time, surv, n.risk, n.event, and n.censor (survival::summary.survfit output format). If flip_surv = TRUE also includes surv.flipped column.

Examples


create_step_curve(x = 1:10)
create_step_curve(x = 1:10, event = rep(0:1, 5))

library(dplyr)
dat = data.frame(x = c(1:10),
                 event = c(1,1,0,1,1,0,0,1,1,1),
                 ptid = c(1,1,2,2,3,3,3,3,3,3))
plot_data <-
 dat %>%
  dplyr::group_by(ptid) %>%
  dplyr::group_modify(~ create_step_curve(x = .x$x, event = .x$event))

ggplot2::ggplot(data = plot_data,
                ggplot2::aes(x = time, y = surv, color = factor(ptid))) +
 ggplot2::geom_step(linetype = "dashed", direction = 'hv', lwd = .35) +
 ggplot2::geom_point(data = plot_data %>% filter(n.censor == 1),
                     shape = 3, size = 6, show.legend = FALSE)

#mAB example for reverse curves
data(CAVD812_mAB)

plot_data <-
CAVD812_mAB %>%
  filter(virus != 'SVA-MLV') %>%
  tidyr::pivot_longer(cols = c(ic50, ic80)) %>%
  dplyr::group_by(name, product) %>%
  dplyr::group_modify(~ create_step_curve(x = pmin(.x$value, 100),
                                          event = as.numeric(.x$value < 50),
                                          flip_surv = TRUE,
                                          flip_top_x = 100))

ggplot2::ggplot(data = plot_data,
                ggplot2::aes(x = time, y = surv.flipped, color = product)) +
  ggplot2::geom_step(direction = 'hv', lwd = .35) +
  ggplot2::scale_x_log10() +
  ggplot2::scale_y_continuous('Viral Coverage (%)') +
  ggplot2::facet_grid(. ~ name) +
  ggplot2::theme_bw()


FredHutch/VISCfunctions documentation built on Oct. 14, 2024, 11:33 p.m.