Description Usage Arguments Details See Also Examples
Plot KaplanMeier or Cox proportional hazards models with atrisk table
using ggplot
.
1 2 3 4 5 6  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 
twoelement numeric vector corresponding to x and ycoordinates
to plot a pvalue; pvalue based on logrank test for significant
differences in KaplanMeier curves (see 
basehaz 
logical; if 
ticks 
threeelement numeric vector corresponding to the "from," "to,"
and "by" arguments of 
median.ticks 
logical; if 
xlab 
xaxis label 
ylab 
yaxis label 
main 
title 
xlim 
twoelement numeric vector of xaxis range; see

ylim 
twoelement numeric vector of yaxis 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., xxpercent chance of containing entire population
of the survival curve which are wider than the pointwise 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 misalign 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54  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
# kaplanmeier
kmfit0 < survfit(Surv(time = time, event = status) ~ 1, data = cancer,
conf.type = 'loglog')
kmfit1 < survfit(Surv(time = time, event = status) ~ sex, data = cancer,
conf.type = 'loglog')
# 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 misaligns 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','5060','6070','70+'),
plot.margin = 3)
## Not run:
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.