| ggsurv {GGally} | R Documentation |
survfit objects using ggplot2This function produces Kaplan-Meier plots using ggplot2.
As a first argument it needs a survfit object, created by the
survival package. Default settings differ for single stratum and
multiple strata objects.
ggsurv(s, CI = "def", plot.cens = TRUE, surv.col = "gg.def", cens.col = "red", lty.est = 1, lty.ci = 2, cens.shape = 3, back.white = FALSE, xlab = "Time", ylab = "Survival", main = "")
s |
an object of class |
CI |
should a confidence interval be plotted? Defaults to |
plot.cens |
mark the censored observations? |
surv.col |
colour of the survival estimate. Defaults to black for
one stratum, and to the default |
cens.col |
colour of the points that mark censored observations. |
lty.est |
linetype of the survival curve(s). Vector length should be either 1 or equal to the number of strata. |
lty.ci |
linetype of the bounds that mark the 95% CI. |
cens.shape |
shape of the points that mark censored observations. |
back.white |
if TRUE the background will not be the default
grey of |
xlab |
the label of the x-axis. |
ylab |
the label of the y-axis. |
main |
the plot label. |
An object of class ggplot
Edwin Thoen edwinthoen@gmail.com
if (require(survival) && require(scales)) {
data(lung, package = "survival")
sf.lung <- survival::survfit(Surv(time, status) ~ 1, data = lung)
ggsurv(sf.lung)
# Multiple strata examples
sf.sex <- survival::survfit(Surv(time, status) ~ sex, data = lung)
pl.sex <- ggsurv(sf.sex)
pl.sex
# Adjusting the legend of the ggsurv fit
pl.sex +
ggplot2::guides(linetype = FALSE) +
ggplot2::scale_colour_discrete(
name = 'Sex',
breaks = c(1,2),
labels = c('Male', 'Female')
)
# We can still adjust the plot after fitting
data(kidney, package = "survival")
sf.kid <- survival::survfit(Surv(time, status) ~ disease, data = kidney)
pl.kid <- ggsurv(sf.kid, plot.cens = FALSE)
pl.kid
# Zoom in to first 80 days
pl.kid <- pl.kid + ggplot2::coord_cartesian(xlim = c(0, 80), ylim = c(0.45, 1))
pl.kid
# Add the diseases names to the plot and remove legend
col <- scales::hue_pal(
h = c(0, 360) + 15,
c = 100,
l = 65,
h.start = 0,
direction = 1
)(4)
pl.kid +
ggplot2::annotate(
"text",
label = c('AN', 'GN', 'Other', 'PKD'),
x = c(50, 20, 50, 71),
y = c(0.47, 0.55, 0.67, 0.8),
size = 5,
colour = col
) +
ggplot2::guides(color = FALSE, linetype = FALSE)
}