Skip to contents

A right-continuous step curve per stratum, censoring ticks, an optional pointwise confidence band, and the number-at-risk table underneath. The table is on by default because a survival curve without one hides how much of its tail rests on a handful of patients, which is where readers over-read it.

Usage

km(
  data,
  groups = NULL,
  group_colors = NULL,
  risk_times = NULL,
  risk_counts = NULL,
  p_label = NULL,
  show_ci = TRUE,
  show_censors = TRUE,
  show_risk_table = TRUE,
  show_legend = TRUE,
  y_from_zero = TRUE,
  line_width = 2,
  x_label = "time",
  y_label = "overall survival",
  theme = NULL,
  width = NULL,
  height = NULL,
  element_id = NULL,
  censor = NULL
)

Arguments

data

Either a survival::survfit object, or a data frame with numeric time and surv columns and optional lower, upper and group columns. Within a stratum, rows must be in ascending time order. A row with a missing time or surv (NA or NaN) is left out with a warning, so the step before it runs on to the next row. An infinite time is not drawn, and a missing or infinite lower or upper leaves a gap in the band; both warn, as in Python, except for the bounds of a survfit, which are NA where the fit has no interval. A survfit stratum is named by its levels alone, "sex=1, ph.ecog=0" becoming "1, 0", when the variable names can be read from the formula of the fit; otherwise the name survfit gives is kept. A survfit of a Cox model with several newdata rows draws one curve per row, named by the row names of newdata ("1", "2", ... unless set).

groups

Character vector fixing the stratum order and colour assignment. Defaults to order of appearance.

group_colors

One hex colour per stratum. NULL uses the component's categorical palette.

risk_times

Numeric vector of times for the at-risk table, also used as the x-axis ticks. NULL picks an even grid across the follow-up.

risk_counts

Integer matrix, strata x risk_times. Computed from a survfit object automatically; required alongside risk_times when you pass a data frame and want the table.

p_label

Optional annotation drawn inside the panel, e.g. "log-rank p = 0.02". Not computed here: pass what your test returned.

show_ci, show_censors, show_risk_table, show_legend

Toggle the confidence band, censoring ticks, at-risk table and legend.

y_from_zero

Start the y axis at zero. Opt-out rather than automatic: zooming y exaggerates separation between curves.

line_width

Curve stroke width in pixels.

x_label, y_label

Axis titles.

theme

Optional named list of theme overrides. Its padding entry is the space in px between the edge of the widget and the plot: one number for every side, or numbers named top, right, bottom and left in a list or vector, a side left out keeping the default of 10. Its categorical entry is an ordered palette: the colours go to the strata in order, and names on the colours are not matched to them. A colour given as a CSS custom property, such as background = "var(--bg)", takes the value the page gives it when the plot draws. After the page changes the property, what the plot drew on a canvas keeps the old colour until it draws again: resize the widget or render it again.

width, height

Widget dimensions (any valid CSS size).

element_id

Optional explicit DOM id.

censor

Optional data frame of censoring ticks, with numeric time and surv columns and an optional group column naming each tick's stratum, as Python's Km(censor=) takes. Column names must match exactly. With a survfit object the ticks are read off the fit unless censor is given. A tick with a missing or infinite time or surv has no place on the plot and is left out with a warning, as in Python. It comes last so the arguments before it keep their positions.

Value

An htmlwidget object.

Details

The widget draws, it does not estimate. Pass a survfit object and the estimates are read off it; pass a data frame and they are used as given. Both routes mean the numbers on screen are the ones your model produced, so a figure rendered here and the same figure rendered by plot() cannot disagree about where a curve steps.

Examples

df <- data.frame(
  time = c(0, 5, 12, 0, 7, 15),
  surv = c(1, 0.9, 0.7, 1, 0.8, 0.5),
  group = rep(c("treated", "control"), each = 3)
)
km(df)