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::survfitobject, or a data frame with numerictimeandsurvcolumns and optionallower,upperandgroupcolumns. Within a stratum, rows must be in ascending time order. A row with a missingtimeorsurv(NAorNaN) is left out with a warning, so the step before it runs on to the next row. An infinitetimeis not drawn, and a missing or infinitelowerorupperleaves a gap in the band; both warn, as in Python, except for the bounds of asurvfit, which areNAwhere the fit has no interval. Asurvfitstratum 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. Asurvfitof a Cox model with severalnewdatarows draws one curve per row, named by the row names ofnewdata("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.
NULLuses the component's categorical palette.- risk_times
Numeric vector of times for the at-risk table, also used as the x-axis ticks.
NULLpicks an even grid across the follow-up.- risk_counts
Integer matrix, strata x
risk_times. Computed from asurvfitobject automatically; required alongsiderisk_timeswhen 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
paddingentry is the space in px between the edge of the widget and the plot: one number for every side, or numbers namedtop,right,bottomandleftin a list or vector, a side left out keeping the default of 10. Itscategoricalentry 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 asbackground = "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
timeandsurvcolumns and an optionalgroupcolumn naming each tick's stratum, as Python'sKm(censor=)takes. Column names must match exactly. With asurvfitobject the ticks are read off the fit unlesscensoris given. A tick with a missing or infinitetimeorsurvhas 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.
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)