ggsurv | R Documentation |
Plot Kaplan-Meier or Cox proportional hazards models with at-risk table
using ggplot
.
ggsurv(
s,
col.surv = 1,
lty.surv = 1,
censor = TRUE,
col.cens = 1,
mark = 3,
confin = TRUE,
confband = FALSE,
col.band = NA,
median = FALSE,
atrisk = TRUE,
col.atrisk,
pval,
basehaz = FALSE,
ticks,
median.ticks = TRUE,
xlab,
ylab,
main,
xlim,
ylim,
legend = "right",
legend.labels,
grid = FALSE,
ggdefault = FALSE,
plot.margin = NULL,
table.margin = NULL,
...
)
s |
|
col.surv |
color of survival lines; should be one color or match number of strata |
lty.surv |
line type used for survival line; default is 1 (solid line) |
censor |
logical; if |
col.cens |
color for censor marks |
mark |
plotting character for censored observations |
confin |
logical; plot confidence bounds around survival estimate |
confband |
logical; plot confidence band; note that this is not a confidence band in the statistical sense; see details |
col.band |
band colors; if |
median |
logical; if |
atrisk |
logical; if |
col.atrisk |
optional color for at risk text, e.g., 'black' |
pval |
two-element numeric vector corresponding to x- and y-coordinates
to plot a p-value; p-value based on log-rank test for significant
differences in Kaplan-Meier curves (see |
basehaz |
logical; if |
ticks |
three-element numeric vector corresponding to the "from," "to,"
and "by" arguments of |
median.ticks |
logical; if |
xlab |
x-axis label |
ylab |
y-axis label |
main |
title |
xlim |
two-element numeric vector of x-axis range; see
|
ylim |
two-element numeric vector of y-axis range; see
|
legend |
legend position (no legend if no strata present); takes values
of |
legend.labels |
labels to use for strata in legend; defaults are made
by |
grid |
logical; if |
ggdefault |
logical; use default ggplot background; if |
plot.margin |
numeric; extra "lines" added to left margin of plot; see details |
table.margin |
numeric; extra "lines" added to left margin of at risk table; see details |
... |
for backwards compatibility with deprecated arguments |
The argument confband = TRUE
does not plot a confidence band in the
statistical sense, i.e., xx-percent chance of containing entire population
of the survival curve which are wider than the point-wise confidence limits.
Rather, it refers to a band of color in ggplot
objects, specifically
the use of a geom_ribbon
geometric shape. The band is
bounded by the confidence limits calculated in the survfit
object
which is passed to ggsurv
in the initial function call.
Long strata labels can mis-align the at risk numbers and plot ticks. If the
arguments plot.margin
and table.margin
are NULL
, the
function will make a guess based on the number of characters in the strata
labels. If this is not perfect, plot.margin
and table.margin
can be specified explicitly by providing a single numeric corresponding to
the number of "lines" of padding (see unit
). Note that the
default for ggplot
is unit(.25, "lines")
.
kmplot
; survival:::plot.survfit
## Not run:
library('ggplot2')
library('grid')
library('gridExtra')
library('survival')
data(cancer)
cancer <- within(cancer, {
age.cat <- factor(as.numeric(cut(age, c(-Inf, 50, 60, 70, Inf))))
meal.cat <- factor(as.numeric(cut(meal.cal,
c(-Inf,quantile(meal.cal, c(.25,.5,.75), na.rm = TRUE), Inf))))
wt.cat <- factor(as.numeric(cut(wt.loss, c(-Inf,quantile(wt.loss,
c(.25,.5,.75), na.rm = TRUE),Inf))))
})
## fitting models
# kaplan-meier
kmfit0 <- survfit(Surv(time = time, event = status) ~ 1, data = cancer,
conf.type = 'log-log')
kmfit1 <- survfit(Surv(time = time, event = status) ~ sex, data = cancer,
conf.type = 'log-log')
# cox proportional hazards
coxfit0 <- survfit(coxph(Surv(time = time, event = status) ~ strata(age.cat),
data = cancer))
coxfit1 <- survfit(coxph(Surv(time = time, event = status) ~ strata(I(age > 45)),
data = cancer))
ggsurv(kmfit0)
ggsurv(kmfit1, confin = FALSE, lty.surv = 1:2,
grid = FALSE, pval = c(500, .75))
ggsurv(kmfit1, confin = FALSE, lty.surv = 1:2,
confband = TRUE, col.band = c('blue','red'),
legend.labels = c('Male', 'Female'),
median = TRUE, ticks = c(0, 1000, 200))
ggsurv(coxfit0, basehaz = TRUE, col.surv = 1:4)
## this long label mis-aligns the table numbers, so we can use plot.margin
## to adjust; it may be easier to adjust plot.margin instead of table.margin
ggsurv(coxfit0, confin = FALSE, col.atrisk = 'black',
col.surv = c('red','green','blue','black'),
legend.labels = c('Less than 50','50-60','60-70','70+'),
plot.margin = 3)
png('plot.png', height = 600, width = 750)
ggsurv(coxfit1, confin = FALSE, median = TRUE, confband = FALSE,
legend.labels = c('< 45','> 45'), legend = FALSE,
col.surv = c('red','green'), mark = '#')
dev.off()
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.