Last data update: 2014.03.03

R: Drawing survival curves using ggplot2
ggsurvplotR Documentation

Drawing survival curves using ggplot2

Description

Drawing survival curves using ggplot2

Usage

ggsurvplot(fit, fun = NULL, color = NULL, palette = "hue",
  break.time.by = NULL, surv.scale = c("default", "percent"),
  conf.int = FALSE, conf.int.fill = "gray", censor = TRUE, pval = FALSE,
  pval.size = 5, pval.coord = c(NULL, NULL), main = NULL, xlab = "Time",
  ylab = "Survival probability", font.main = c(16, "plain", "black"),
  font.x = c(14, "plain", "black"), font.y = c(14, "plain", "black"),
  font.tickslab = c(12, "plain", "black"), xlim = NULL, ylim = NULL,
  legend = c("top", "bottom", "left", "right", "none"),
  legend.title = "Strata", legend.labs = NULL, font.legend = c(10,
  "plain", "black"), risk.table = FALSE,
  risk.table.title = "Number at risk by time", risk.table.col = "black",
  risk.table.fontsize = 4.5, risk.table.y.text = TRUE,
  risk.table.y.text.col = TRUE, risk.table.height = 0.25,
  surv.plot.height = 0.75, ggtheme = theme_classic2(), ...)

## S3 method for class 'ggsurvplot'
print(x, surv.plot.height = NULL,
  risk.table.height = NULL, ...)

Arguments

fit

an object of class survfit.

fun

an arbitrary function defining a transformation of the survival curve. For example use function(y)y*100. Often used transformations can be specified with a character argument instead: "event" plots cumulative events (f(y) = 1-y), "cumhaz" plots the cumulative hazard function (f(y) = -log(y)), and "cloglog" creates a complimentary log-log survival plot (f(y) = log(-log(y)) along with log scale for the x-axis).

color

color to be used for the survival curves. This argument is ignored when the number of strata (groups > 1). In this case, use the argument palette.

palette

the color palette to be used. Allowed values include "hue" for the default hue color scale; "grey" for grey color palettes; brewer palettes e.g. "RdBu", "Blues", ...; or custom color palette e.g. c("blue", "red").

break.time.by

numeric value controlling time axis breaks. Default value is NULL.

surv.scale

scale transformation of survival curves. Allowed values are "default" or "percent".

conf.int

logical value. If TRUE, plots confidence interval.

conf.int.fill

fill color to be used for confidence interval.

censor

logical value. If TRUE, censors will be drawn.

pval

logical value. If TRUE, the p-value is added on the plot.

pval.size

numeric value specifying the p-value text size. Default is 5.

pval.coord

numeric vector, of length 2, specifying the x and y coordinates of the p-value. Default values are NULL.

main, xlab, ylab

main title and axis labels

font.main, font.x, font.y, font.tickslab, font.legend

a vector of length 3 indicating respectively the size (e.g.: 14), the style (e.g.: "plain", "bold", "italic", "bold.italic") and the color (e.g.: "red") of main title, xlab and ylab and axis tick labels, respectively. For example font.x = c(14, "bold", "red"). Use font.x = 14, to change only font size; or use font.x = "bold", to change only font face.

xlim, ylim

x and y axis limits e.g. xlim = c(0, 1000), ylim = c(0, 1).

legend

character specifying legend position. Allowed values are one of c("top", "bottom", "left", "right", "none"). Default is "top" side position. to remove the legend use legend = "none". Legend position can be also specified using a numeric vector c(x, y); see details section.

legend.title

legend title.

legend.labs

character vector specifying legend labels. Used to replace the names of the strata from the fit. Should be given in the same order as those strata.

risk.table

logical value specifying whether to show risk table. Default is FALSE.

risk.table.title

Title to be used for risk table.

risk.table.col

color to be used for risk table. Default value is "black". If you want to color by strata (i.e. groups), use risk.table.col = "strata".

risk.table.fontsize

font size to be used for the risk table.

risk.table.y.text

logical. Default is TRUE. If FALSE, risk table y axis tick labels will be hidden.

risk.table.y.text.col

logical. Default value is FALSE. If TRUE, risk table tick labels will be colored by strata.

risk.table.height

the height of the risk table on the grid. Increase the value when you have many strata. Default is 0.25. Ignored when risk.table = FALSE.

surv.plot.height

the height of the survival plot on the grid. Default is 0.75. Ignored when risk.table = FALSE.

ggtheme

function, ggplot2 theme name. Default value is survminer::theme_classic2(). Allowed values include ggplot2 official themes: theme_gray(), theme_bw(), theme_minimal(), theme_classic(), theme_void(), ....

...

other arguments to be passed to ggplot2 geom_*() functions such as linetype, size, ...

x

an object of class ggsurvplot

Details

legend position: The argument legend can be also a numeric vector c(x,y). In this case it is possible to position the legend inside the plotting area. x and y are the coordinates of the legend box. Their values should be between 0 and 1. c(0,0) corresponds to the "bottom left" and c(1,1) corresponds to the "top right" position. For instance use legend = c(0.8, 0.2).

Value

return an object of class ggsurvplot which is list containing two ggplot objects, including:

  • plot: the survival plot

  • table: the number at risk table per time

Functions

  • ggsurvplot: Draws survival curves using ggplot2.

Author(s)

Alboukadel Kassambara, alboukadel.kassambara@gmail.com

Examples


#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Example 1: Survival curves with two groups
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Fit survival curves
#++++++++++++++++++++++++++++++++++++
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)

# Drawing survival curves
ggsurvplot(fit)

# Change font size, style and color
#++++++++++++++++++++++++++++++++++++
## Not run: 
# Change font size, style and color at the same time
ggsurvplot(fit, main = "Survival curve",
   font.main = c(16, "bold", "darkblue"),
   font.x = c(14, "bold.italic", "red"),
   font.y = c(14, "bold.italic", "darkred"),
   font.tickslab = c(12, "plain", "darkgreen"))

## End(Not run)

# Legend: title, labels and position
#++++++++++++++++++++++++++++++++++++

# Change the legend title and labels
ggsurvplot(fit, legend = "bottom",
          legend.title = "Sex",
          legend.labs = c("Male", "Female"))

# Specify legend position by its coordinates
ggsurvplot(fit, legend = c(0.2, 0.2))


# format
#++++++++++++++++++++++++++++++++++++
# change line size --> 1
# Change line types by groups (i.e. "strata")
# and change color palette
ggsurvplot(fit,  size = 1,  # change line size
          linetype = "strata", # change line type by groups
          break.time.by = 250, # break time axis by 250
          palette = c("#E7B800", "#2E9FDF"), # custom color palette
          conf.int = TRUE, # Add confidence interval
          pval = TRUE # Add p-value
)

# Use brewer color palette "Dark2"
# Add risk table
ggsurvplot(fit, linetype = "strata",
          conf.int = TRUE, pval = TRUE,
          palette = "Dark2", risk.table = TRUE)


# Change color, linetype by strata, risk.table color by strata
ggsurvplot(fit,
          pval = TRUE, conf.int = TRUE,
          risk.table = TRUE, # Add risk table
          risk.table.col = "strata", # Change risk table color by groups
          lienetype = "strata", # Change line type by groups
          ggtheme = theme_bw(), # Change ggplot2 theme
          palette = c("#E7B800", "#2E9FDF"))


#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Example 3: Survival curve with multiple group
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Fit (complexe) survival curves
#++++++++++++++++++++++++++++++++++++
## Not run: 
require("survival")
fit2 <- survfit( Surv(time, status) ~ rx + adhere,
                data = colon )

# Visualize
#++++++++++++++++++++++++++++++++++++

# Visualize: add p-value, chang y limits
# change color using brewer palette
# Adjust risk table and survival plot heights
ggsurvplot(fit2, pval = TRUE,
          break.time.by = 400,
          risk.table = TRUE,
          risk.table.col = "strata",
          risk.table.height = 0.5, # Useful when you have multiple groups
          palette = "Dark2")

## End(Not run)

Results