kmplot | R Documentation |
Plot Kaplan-Meier or Cox regression models with optional at-risk table, median survival summary, confidence bands/intervals, pairwise tests, hazard ratios, cumulative incidences, and other features.
kmplot(
object,
data = NULL,
lty.surv = par("lty"),
lwd.surv = par("lwd"),
col.surv = seq_along(object$n),
mark = 3L,
lwd.mark = lwd.surv,
lty.ci = 0,
lwd.ci = lwd.surv,
col.ci = col.surv,
col.band = FALSE,
alpha.band = 0.5,
kmdiff = FALSE,
atrisk.table = TRUE,
atrisk.lab = NULL,
atrisk.pad = 1,
atrisk.type = c("atrisk", "events", "atrisk-events", "survival", "survival-ci",
"percent", "percent-ci", "cuminc", "percent-cuminc", "cuminc-ci",
"percent-cuminc-ci"),
atrisk.digits = (!grepl("percent", atrisk.type)) * 2,
atrisk.lines = TRUE,
atrisk.lines.adj = NA,
atrisk.col = !atrisk.lines,
atrisk.min = NULL,
strata.lab = NULL,
strata.expr = NULL,
strata.order = seq_along(object$n),
extra.margin = 5,
mar = NULL,
median = FALSE,
digits.median = 0L,
ci.median = TRUE,
args.median = list(),
xaxs = "s",
xlim = NULL,
ylim = NULL,
xaxis.at = pretty(xlim),
xaxis.lab = xaxis.at,
atrisk.at = xaxis.at,
yaxis.at = pretty(0:1),
yaxis.lab = yaxis.at,
xlab = "Time",
ylab = "Probability",
main = NULL,
line.xlab = 2.5,
cex.axis = par("cex.axis"),
cex.atrisk = cex.axis,
cex.lab = par("cex.lab"),
cex.main = par("cex.main"),
legend = !atrisk.table && !is.null(object$strata),
args.legend = list(),
median.legend = FALSE,
lr_test = TRUE,
tt_test = FALSE,
test_details = TRUE,
args.test = list(),
hr_text = FALSE,
args.hr = list(),
events = FALSE,
pw_test = FALSE,
args.pw = list(),
format_pval = TRUE,
stratify = NULL,
fun = c("S", "F"),
times = NULL,
times.lab = NULL,
times.type = c("percent", "atrisk", "events", "atrisk-events", "survival",
"survival-ci", "percent-ci", "cuminc", "percent-cuminc", "cuminc-ci",
"percent-cuminc-ci"),
times.digits = (!grepl("percent", times.type)) * 2,
args.times = list(),
add = FALSE,
panel.first = NULL,
panel.last = NULL,
...
)
object |
an object of class |
data |
(optional) the data frame used to create |
lty.surv , lwd.surv , col.surv |
line type, width, and color for survival curve(s); colors may be either numeric, color names as character string(s), or hexadecimal string(s) colors will be assigned to curves in order of strata or may be mapped
using a named vector (the names must match the ones given to
|
mark , lwd.mark |
numeric plotting character ( |
lty.ci , lwd.ci , col.ci |
line type, width, and color for confidence
interval(s); not plotted (i.e., |
col.band |
color for confidence band(s); either as numeric, color
string(s), or hexadecimal string(s); if |
alpha.band |
alpha transparency (in |
kmdiff |
logical; if |
atrisk.table |
logical; if |
atrisk.lab |
heading for at-risk table |
atrisk.pad |
extra padding between plot and at-risk table; alternatively, a vector of padding for each line in the at-risk table, recycled as needed |
atrisk.type |
a character string giving the type of "at-risk" table to
show; one of |
atrisk.digits |
when survival probabilities are shown in at-risk table
(see |
atrisk.lines |
logical; draw lines next to strata in at-risk table |
atrisk.lines.adj |
a vector of length 2 giving the left and right
adjustment for the at-risk lines in normalized units, e.g.,
|
atrisk.col |
logical or a vector with colors for at-risk table text;
if |
atrisk.min |
optional integer to replace any at-risk counts
|
strata.lab |
labels used in legend and at-risk table for strata; if
|
strata.expr |
an alternative to |
strata.order |
order of strata in legend and at-risk table |
extra.margin |
increase left margin when strata labels in at-risk
table are long (note that this will be overridden by |
mar |
margins; see |
median |
logical or numeric; if |
digits.median |
number of digits past the decimal point to keep for median(s) |
ci.median |
logical; if |
args.median |
an optional named list of additionally, |
xaxs |
style of axis; see details or |
xlim , ylim |
x- and y-axis limits |
xaxis.at , yaxis.at |
positions for x- and y-axis labels and ticks |
xaxis.lab , yaxis.lab |
x- and y-axis tick labels |
atrisk.at |
x-coordinates to show at-risk table (default is
|
xlab , ylab |
x- and y-axis labels |
main |
title of plot |
line.xlab |
line position for |
cex.axis , cex.atrisk |
text size for axis tick labels and at-risk table |
cex.lab , cex.main |
text size for axis and main titles |
legend |
logical, a vector of x/y coordinates, or a keyword (see
|
args.legend |
an optional named list of |
median.legend |
logical; if |
lr_test |
logical or numeric; if |
tt_test |
logical; if |
test_details |
logical; if |
args.test |
an optional named list of |
hr_text |
logical; if |
args.hr |
an optional named list of |
events |
logical; if |
pw_test |
logical; if |
args.pw |
an optional named list of |
format_pval |
logical; if |
stratify |
(dev) |
fun |
survival curve transformation, one of |
times |
time points to show additional summaries in a table added to the plot (requires plotr) |
times.lab |
heading for the |
times.type |
see |
times.digits |
when survival probabilities are shown in times table
(see |
args.times |
an optional named list of |
add |
logical; if |
panel.first |
an expression to be evaluated after the plot axes are set up but before any plotting takes place |
panel.last |
an expression to be evaluated after plotting but before returning from the function |
... |
additional parameters ( |
Line specifications (e.g., lty.surv
, lwd.surv
, etc.) will be
recycled as needed.
If col.band
is not NULL
, NA
, or FALSE
, a
confidence band is plotted; however, this is not a confidence band in the
statistical sense, i.e., a xx-percent chance of containing the entire
population of the survival curve which are wider than the point-wise
confidence limits. Rather, it refers to a band of color plotted between
the confidence limits calculated in the survfit object. That is, the xx-%
confidence interval (plotted when lty.ci != 0
) and the confidence
bands are identical–just two ways of plotting the same invervals.
xaxs
is the style of the x-axis; see par
. The default
for kmplot
is "S"
which is equivalent to xaxs = "i"
but with the maximum xlim
value increased by 4%. Other styles for
xaxs
currently implemented in R
are "r"
(default for
plotting and the previous value for kmplot
) and "i"
which
will not add padding to the ends of the axes.
When saving plots, it is highly recommended to use png
,
svg
, pdf
, etc. instead of exporting directly
from the R
graphics device. Doing so may cause the at-risk table or
legend to be mis-aligned.
Adapted from http://biostat.mc.vanderbilt.edu/wiki/Main/TatsukiRcode
kmplot_by
; survival:::plot.survfit
;
kmplot_data_
; atrisk_data_
;
lr_text
; lr_pval
; points.kmplot
;
kmdiff
; ggsurv
library('survival')
km1 <- survfit(Surv(time, status) ~ sex, data = colon)
km2 <- survfit(Surv(time, status) ~ I(rx == "Obs") + adhere, data = colon)
## basic usage
kmplot(km1)
kmplot(km1, args.median = list(x = 500, y = 0.2))
kmplot(km1, atrisk.table = FALSE, median.legend = TRUE)
kmplot(km1, fun = 'F')
kmplot(km1, atrisk.col = c('grey50', 'tomato'), test_details = FALSE,
args.test = list(col = 'red', cex = 1.5, .prefix = 'Log-rank: '))
kmplot(km1, mark = 'bump', atrisk.lines = FALSE, median = TRUE)
kmplot(km1, mark = 'bump', atrisk.lines = FALSE, median = 3500)
## atrisk spacing adjustment
kmplot(km2, atrisk.pad = 3)
kmplot(km2, atrisk.pad = 1:4 / 4)
kmplot(km2, atrisk.table = FALSE, lwd.surv = 2, lwd.mark = 0.5,
col.surv = 1:4, col.band = c(1, 0, 0, 4))
## table of additional stats
kmplot(km1, times = 1:3 * 1000, times.type = 'percent-ci')
kmplot(km1, times = 1:3 * 1000, times.type = 'percent-ci',
atrisk.table = FALSE, args.times = list(x = 'bottomright'))
kmplot(km1, times = 0:10 * 250, times.type = 'survival',
times.digits = 3, times.lab = 'Prob. of survival',
args.times = list(x = mean(par('usr')[1]), y = 0))
## use stratified models
kmplot(km1, stratify = 'differ')
kmplot(km1, stratify = c('differ', 'adhere'))
## for hazard ratios, use factors to fit the proper cox model
kmplot(survfit(Surv(time, status) ~ factor(sex), data = colon),
hr_text = TRUE, strata.lab = TRUE)
kmplot(survfit(Surv(time, status) ~ interaction(sex, rx), data = colon),
hr_text = TRUE, atrisk.table = FALSE, legend = FALSE)
kmplot(survfit(Surv(time, status) ~ interaction(sex, rx), data = colon),
hr_text = TRUE, atrisk.table = FALSE, legend = FALSE,
format_pval = function(x) format(x, digits = 2, scipen = 0))
## at-risk and p-value options
kmplot(km2, tt_test = TRUE, test_details = FALSE)
kmplot(km2, tt_test = TRUE, format_pval = format.pval)
kmplot(km1, atrisk.type = 'survival', atrisk.digits = 3L)
kmplot(km1, atrisk.type = 'atrisk-events')
kmplot(survfit(Surv(time, status) ~ rx, data = colon),
pw_test = TRUE, args.pw = list(text.col = 1:3, x = 'bottomleft'))
## Not run:
## expressions in at-risk table (strata.expr takes precedence)
kmplot(km1, strata.lab = c('\u2640', '\u2642'))
kmplot(km1, strata.lab = c('\u2640', '\u2642'),
strata.expr = expression(widetilde(ring(Female)),
phantom() >= Male))
## End(Not run)
## character vectors passed to strata.expr will be parsed
kmplot(km1, strata.expr = c('Sex[Female]', 'Sex[Male]'))
## but not those passed to strata.lab
kmplot(km1, strata.lab = c('Sex[Female]', 'Sex[Male]'))
## when using mfrow options, use add = TRUE and mar to align axes
mar <- c(9, 6, 2, 2)
op <- par(mfrow = c(1, 2))
kmplot(km1, add = TRUE, mar = mar)
kmplot(km2, add = TRUE, strata.lab = TRUE, mar = mar)
par(mfrow = op)
## survfit.coxph object
cx1 <- coxph(Surv(time, status) ~ sex, data = colon)
cx2 <- coxph(Surv(time, status) ~ sex + adhere, data = colon)
cx3 <- coxph(Surv(time, status) ~ I(rx == "Obs") + adhere + age, data = colon)
kmplot(survfit(cx1))
kmplot(survfit(cx1, data.frame(sex = 0:1)), strata.lab = c('F', 'M'))
nd <- expand.grid(adhere = 0:1, sex = 0:1)
kmplot(survfit(cx2, nd), col.surv = 1:4, strata.lab = do.call(paste, nd))
## compare
kmplot(survfit(Surv(time, status) ~ sex + adhere, data = colon),
atrisk.type = 'survival')
nd <- data.frame(rx = 'Obs', adhere = 0:1, age = c(30, 30, 70, 70))
kmplot(survfit(cx3, nd), col.surv = 1:4, strata.lab = do.call(paste, nd))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.