kmplot_by | R Documentation |
This function helps create stratified kmplot
s quickly with
panel labels, survival curve test(s), and/or Cox regression summaries for
each plot.
data
should have at least three variables: strata
,
*_time
, and *_ind
where *
is event
. For
example, to use progression-free survival, data
should have columns
"pfs_time"
and "pfs_ind"
(in this case the user should use
event = 'pfs'
) and optionally the strata
column unless only
the null model (strata = "1"
is needed (default).
Alternatively, the time
argument may be used instead of following
the above; in this case, time
and event
must be variable
names in data
. However, the method described above is more
efficient and preferred.
kmplot_by(
strata = "1",
event = NULL,
data = NULL,
by = NULL,
time = NULL,
single = TRUE,
lr_test = TRUE,
main = NULL,
ylab = NULL,
sub = NULL,
strata_lab = NULL,
fig_lab = NULL,
col.surv = NULL,
map.col = FALSE,
legend = FALSE,
add = FALSE,
plot = TRUE,
args.survfit = list(),
stratify = NULL,
panel.first = NULL,
panel.last = NULL,
...
)
strata , event , time , by |
character strings of the strata, event (pfs, os, ttp, etc.; see details), time (optional), and stratification variables; additionally, vectors for each are allowed |
data |
a data frame |
single |
logical; if |
lr_test |
logical or numeric; if |
main |
title of plot(s) |
ylab |
y-axis label |
sub |
sub-title displayed in upper left corner; should be a character
vector with length equal to the number of panels (i.e., the number of
unique values of |
strata_lab |
at-risk table strata labels; should be a character vector
with length equal to the number of strata; |
fig_lab |
figure panel labels; should be a character vector with
length equal to the number of panels (i.e., the number of unique values of
|
col.surv |
color for individual survival curves or for all curves in
a plot if |
map.col |
logical; if |
legend |
logical, a vector of x/y coordinates, or a keyword (see
|
add |
logical; if |
plot |
logical; if |
args.survfit |
a named list of optional arguments passed to
|
stratify |
(dev) |
panel.first , panel.last , ... |
additional arguments passed to
|
Invisibly returns a list of survfit
object(s) used to generate
plot(s). If by
was used, there will be a list element for each unique
value.
kmplot
; survdiff
; kmplot_data_
;
lr_text
; lr_pval
; points.kmplot
library('survival')
kmplot_by(time = 'time', event = 'status', data = colon)
with(colon, kmplot_by('All', time = time, event = status))
## create *_ind, *_time variables, see details
colon2 <- within(colon[duplicated(colon$id), ], {
pfs_time <- time
pfs_ind <- status
sex <- factor(sex, 0:1, c('Female','Male'))
})
kmplot_by('rx', 'pfs', data = colon2, col.surv = 1:3,
strata_lab = FALSE, median = TRUE)
kmplot_by('rx', 'pfs', data = colon2, col.surv = 1:3,
strata_lab = FALSE, median = TRUE, args.survfit = list(conf.int = 0.90))
## these are equivalent (with minor differences in aesthetics)
kmplot_by(time = 'pfs_time', event = 'pfs_ind', data = colon2)
kmplot_by('1', 'pfs', colon2)
## return value is a list of survfit objects
l <- kmplot_by('sex', 'pfs', colon2, 'rx', plot = FALSE)
str(lapply(l, kmplot))
str(lapply(l, kmplot_by))
## multiple variables can be combined
kmplot_by('rx + sex', 'pfs', colon2, strata_lab = FALSE, lty.surv = 1:6)
## if "by" is given, default is to plot separately
kmplot_by('rx', 'pfs', colon2, by = 'sex', col.surv = 1:3,
strata_lab = c('Observation', 'Trt', 'Trt + 5-FU'))
## if "by" is given, use map.col to map colors to plots
kmplot_by( 'rx', 'pfs', colon2, by = 'rx', map.col = TRUE, single = FALSE)
kmplot_by('sex', 'pfs', colon2, by = 'rx', map.col = TRUE, single = FALSE)
kmplot_by('sex', 'pfs', colon2, by = 'age > 60', map.col = TRUE, single = FALSE)
## to ensure colors are mapped to the same strata across plots (eg, if
## all sub plots do not have the same groups), use a _named_ vector
kmplot_by('rx', 'pfs', colon2, by = 'rx', xlim = c(0, 3000),
col.surv = c(Lev = 'brown', Obs = 'blue', 'Lev+5FU' = 'purple'))
## if single = FALSE, uses n2mfrow function to set par('mfrow')
kmplot_by('rx', 'pfs', colon2, by = 'sex', col.surv = 1:3, single = FALSE,
strata_lab = c('Observe', 'Trt', 'Trt + 5-FU'), main = levels(colon2$sex))
## if par('mfrow') is anything other than c(1,1), uses current setting
par(mfrow = c(2,2))
kmplot_by('rx', 'pfs', colon2, by = 'sex', col.surv = 1:3, single = FALSE,
strata_lab = c('Observation','Trt','Trt + 5-FU'), add = TRUE)
kmplot_by('1', 'pfs', colon2, by = 'sex', col.surv = 1:3, single = FALSE,
strata_lab = FALSE, fig = c('C', 'D'), lr_test = FALSE)
## use add = TRUE to add to a figure region without using the by argument
par(mfrow = c(1, 2))
mar <- c(8, 6, 3, 2) ## to align axes
kmplot_by( 'rx', 'pfs', colon2, strata_lab = FALSE, add = TRUE, mar = mar)
kmplot_by('sex', 'pfs', colon2, strata_lab = FALSE, add = TRUE, mar = mar)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.