We recently released the survminer verion 0.3, which includes many new features to help in **visualizing** and **sumarizing** **survival analysis** results.

In this article, we present a cheatsheet for survminer, created by Przemysław Biecek, and provide an overview of main functions.

The cheatsheet can be downloaded from STHDA and from Rstudio. It contains selected important functions, such as:

**ggsurvplot**() for plotting survival curves**ggcoxzph**() and**ggcoxdiagnostics**() for assessing the assumtions of the Cox model**ggforest**() and**ggcoxadjustedcurves**() for summarizing a Cox model

Additional functions, that you might find helpful, are briefly described in the next section.

The main functions, in the package, are organized in different categories as follow.

**ggsurvplot**(): Draws survival curves with the ‘number at risk’ table, the cumulative number of events table and the cumulative number of censored subjects table.**arrange_ggsurvplots**(): Arranges multiple ggsurvplots on the same page.**ggsurvevents**(): Plots the distribution of event’s times.**surv_summary**(): Summary of a survival curve. Compared to the default summary() function, surv_summary() creates a data frame containing a nice summary from survfit results.**surv_cutpoint**(): Determines the optimal cutpoint for one or multiple continuous variables at once. Provides a value of a cutpoint that correspond to the most significant relation with survival.**pairwise_survdiff**(): Multiple comparisons of survival curves. Calculate pairwise comparisons between group levels with corrections for multiple testing.

**ggcoxzph**(): Graphical test of proportional hazards. Displays a graph of the scaled Schoenfeld residuals, along with a smooth curve using ggplot2. Wrapper around plot.cox.zph().**ggcoxdiagnostics**(): Displays diagnostics graphs presenting goodness of Cox Proportional Hazards Model fit.**ggcoxfunctional**(): Displays graphs of continuous explanatory variable against martingale residuals of null cox proportional hazards model. It helps to properly choose the functional form of continuous variable in cox model.

**ggforest**(): Draws forest plot for CoxPH model.**ggcoxadjustedcurves**(): Plots adjusted survival curves for coxph model.

**ggcompetingrisks**(): Plots cumulative incidence curves for competing risks.

Find out more at http://www.sthda.com/english/rpkgs/survminer/, and check out the documentation and usage examples of each of the functions in survminer package.

This analysis has been performed using **R software** (ver. 3.3.2).

I’m very pleased to announce that **survminer 0.3.0** is now available on CRAN. survminer makes it easy to create elegant and informative **survival curves**. It includes also functions for summarizing and inspecting graphically the **Cox proportional hazards model assumptions**.

This is a big release and a special thanks goes to Marcin Kosiński and Przemysław Biecek for their great works in actively improving and adding new features to the survminer package. The official online documentation is available at http://www.sthda.com/english/rpkgs/survminer/.

- Release notes
- Installing and loading survminer
- Survival curves
- Arranging multiple ggsurvplots on the same page
- Distribution of events’ times
- Adjusted survival curves for Cox model
- Graphical summary of Cox model
- Pairwise comparisons for survival curves
- Visualizing competing risk analysis
- Related articles
- Infos

In this post, we present only the most important changes in v0.3.0. See the release notes for a complete list.

**data**: Now, it’s recommended to specify the data used to compute survival curves (#142). This will avoid the error generated when trying to use the*ggsurvplot()*function inside another functions (@zzawadz, #125).**cumevents**and**cumcensor**: logical value for displaying the cumulative number of events table (#117) and the cumulative number of censored subjects table (#155), respectively.**tables.theme**for changing the theme of the tables under the main plot.**pval.method**and**log.rank.weights**: New possibilities to compare survival curves. Functionality based on**survMisc::comp**(@MarcinKosinski, #17). Read also the following blog post on R-Addict website: Comparing (Fancy) Survival Curves with Weighted Log-rank Tests.

**pairwise_survdiff**() for pairwise comparisons of survival curves (#97).**arrange_ggsurvplots**() to arrange multiple ggsurvplots on the same page (#66)

Thanks to the work of Przemysław Biecek, survminer 0.3.0 has received four new functions:

**ggsurvevents**() to plot the distribution of event’s times (@pbiecek, #116).**ggcoxadjustedcurves**() to plot adjusted survival curves for Cox proportional hazards model (@pbiecek, #133 & @markdanese, #67).**ggforest**() to draw a forest plot (i.e. graphical summary) for the Cox model (@pbiecek, #114).**ggcompetingrisks**() to plot the cumulative incidence curves for competing risks (@pbiecek, #168).

Two new vignettes were contributed by Marcin Kosiński:

Install the latest developmental version from GitHub:

```
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/survminer", build_vignettes = TRUE)
```

Or, install the latest release from CRAN as follow:

`install.packages("survminer")`

To load survminer in R, type this:

`library("survminer")`

New arguments displaying supplementary survival tables - *cumulative events & censored subjects* - under the main survival curves:

**risk.table = TRUE**: Displays the risk table**cumevents = TRUE**: Displays the cumulative number of events table.**cumcensor = TRUE**: Displays the cumulative number of censoring table.**tables.height = 0.25**: Numeric value (in [0 - 1]) to adjust the height of all tables under the main survival plot.

```
# Fit survival curves
require("survival")
fit <- survfit(Surv(time, status) ~ sex, data = lung)
# Plot informative survival curves
library("survminer")
ggsurvplot(fit, data = lung,
title = "Survival Curves",
pval = TRUE, pval.method = TRUE, # Add p-value & method name
surv.median.line = "hv", # Add median survival lines
legend.title = "Sex", # Change legend titles
legend.labs = c("Male", "female"), # Change legend labels
palette = "jco", # Use JCO journal color palette
risk.table = TRUE, # Add No at risk table
cumevents = TRUE, # Add cumulative No of events table
tables.height = 0.15, # Specify tables height
tables.theme = theme_cleantable(), # Clean theme for tables
tables.y.text = FALSE # Hide tables y axis text
)
```

Cumulative events and censored tables are good additional feedback to survival curves, so that one could realize: what is the number of risk set AND what is the cause that the risk set become smaller: is it caused by events or by censored events?

The function **arrange_ggsurvplots**() [in *survminer*] can be used to arrange multiple ggsurvplots on the same page.

```
# List of ggsurvplots
splots <- list()
splots[[1]] <- ggsurvplot(fit, data = lung,
risk.table = TRUE,
tables.y.text = FALSE,
ggtheme = theme_light())
splots[[2]] <- ggsurvplot(fit, data = lung,
risk.table = TRUE,
tables.y.text = FALSE,
ggtheme = theme_grey())
# Arrange multiple ggsurvplots and print the output
arrange_ggsurvplots(splots, print = TRUE,
ncol = 2, nrow = 1, risk.table.height = 0.25)
```

If you want to save the output into a pdf, type this:

```
# Arrange and save into pdf file
res <- arrange_ggsurvplots(splots, print = FALSE)
ggsave("myfile.pdf", res)
```

The function **ggsurvevents**() [in *survminer*] calculates and plots the distribution for events (both status = 0 and status = 1). It helps to notice when censoring is more common (@pbiecek, #116). This is an alternative to cumulative events and censored tables, described in the previous section.

For example in colon dataset, as illustrated below, censoring occur mostly after the 6’th year:

```
require("survival")
surv <- Surv(colon$time, colon$status)
ggsurvevents(surv)
```

Adjusted survival curves show how a selected factor influences survival estimated from a cox model. If you want read more about why we need to adjust survival curves, see this document: Adjusted survival curves.

Briefly, in clinical investigations, there are many situations, where several known factors, potentially affect patient prognosis. For example, suppose two groups of patients are compared: those with and those without a specific genotype. If one of the groups also contains older individuals, any difference in survival may be attributable to genotype or age or indeed both. Hence, when investigating survival in relation to any one factor, it is often desirable to adjust for the impact of others.

The cox proportional-hazards model is one of the most important methods used for modelling survival analysis data.

Here, we present the function **ggcoxadjustedcurves**() [in *survminer*] for plotting adjusted survival curves for cox proportional hazards model. The **ggcoxadjustedcurves**() function models the risks due to the confounders as described in the section 5.2 of this article: Terry M Therneau (2015); Adjusted survival curves. Briefly, the key idea is to predict survival for all individuals in the cohort, and then take the average of the predicted curves by groups of interest (for example, sex, age, genotype groups, etc.).

```
# Data preparation and computing cox model
library(survival)
lung$sex <- factor(lung$sex, levels = c(1,2),
labels = c("Male", "Female"))
res.cox <- coxph(Surv(time, status) ~ sex + age + ph.ecog, data = lung)
# Plot the baseline survival function
# with showing all individual predicted surv. curves
ggcoxadjustedcurves(res.cox, data = lung,
individual.curves = TRUE)
```

```
# Adjusted survival curves for the variable "sex"
ggcoxadjustedcurves(res.cox, data = lung,
variable = lung[, "sex"], # Variable of interest
legend.title = "Sex", # Change legend title
palette = "npg", # nature publishing group color palettes
curv.size = 2 # Change line size
)
```

The function **ggforest**() [in *survminer*] can be used to create a graphical summary of a Cox model, also known as forest plot. For each covariate, it displays the hazard ratio (HR) and the 95% confidence intervals of the HR. By default, covariates with significant p-value are highlighted in red.

```
# Fit a Cox model
library(survival)
res.cox <- coxph(Surv(time, status) ~ sex + age + ph.ecog, data = lung)
res.cox
```

```
## Call:
## coxph(formula = Surv(time, status) ~ sex + age + ph.ecog, data = lung)
##
## coef exp(coef) se(coef) z p
## sexFemale -0.55261 0.57544 0.16774 -3.29 0.00099
## age 0.01107 1.01113 0.00927 1.19 0.23242
## ph.ecog 0.46373 1.58999 0.11358 4.08 4.4e-05
##
## Likelihood ratio test=30.5 on 3 df, p=1.08e-06
## n= 227, number of events= 164
## (1 observation deleted due to missingness)
```

```
# Create a forest plot
ggforest(res.cox)
```

When you compare three or more survival curves at once, the function **survdiff**() [in *survival* package] returns a global p-value whether to reject or not the null hypothesis.

With this, you know that a difference exists between groups, but you don’t know where. You can’t know until you test each combination.

Therefore, we implemented the function **pairwise_survdiff**() [in *survminer*]. It calculates pairwise comparisons between group levels with corrections for multiple testing.

**Multiple survival curves with global p-value:**

```
library("survival")
library("survminer")
# Survival curves with global p-value
data(myeloma)
fit2 <- survfit(Surv(time, event) ~ molecular_group, data = myeloma)
ggsurvplot(fit2, data = myeloma,
legend.title = "Molecular Group",
legend.labs = levels(myeloma$molecular_group),
legend = "right",
pval = TRUE, palette = "lancet")
```

**Pairwise survdiff:**

```
# Pairwise survdiff
res <- pairwise_survdiff(Surv(time, event) ~ molecular_group,
data = myeloma)
res
```

```
##
## Pairwise comparisons using Log-Rank test
##
## data: myeloma and molecular_group
##
## Cyclin D-1 Cyclin D-2 Hyperdiploid Low bone disease MAF MMSET
## Cyclin D-2 0.723 - - - - -
## Hyperdiploid 0.328 0.103 - - - -
## Low bone disease 0.644 0.447 0.723 - - -
## MAF 0.943 0.723 0.103 0.523 - -
## MMSET 0.103 0.038 0.527 0.485 0.038 -
## Proliferation 0.723 0.988 0.103 0.485 0.644 0.062
##
## P value adjustment method: BH
```

**Symbolic number coding:**

```
# Symbolic number coding
symnum(res$p.value, cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 0.1, 1),
symbols = c("****", "***", "**", "*", "+", " "),
abbr.colnames = FALSE, na = "")
```

```
## Cyclin D-1 Cyclin D-2 Hyperdiploid Low bone disease MAF MMSET
## Cyclin D-2
## Hyperdiploid
## Low bone disease
## MAF
## MMSET * *
## Proliferation +
## attr(,"legend")
## [1] 0 '****' 1e-04 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1 \t ## NA: ''
```

Competing risk events refer to a situation where an individual (patient) is at risk of more than one mutually exclusive event, such as death from different causes, and the occurrence of one of these will prevent any other event from ever happening.

For example, when studying relapse in patients who underwent HSCT (Hematopoietic stem cell transplantation), transplant related mortality is a competing risk event and the cumulative incidence function (CIF) must be calculated by appropriate accounting.

A ‘competing risks’ analysis is implemented in the R package cmprsk. Here, we provide the **ggcompetingrisks**() function [in *survminer*] to plot the results using ggplot2-based elegant data visualization.

```
# Create a demo data set
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set.seed(2)
failure_time <- rexp(100)
status <- factor(sample(0:2, 100, replace=TRUE), 0:2,
c('no event', 'death', 'progression'))
disease <- factor(sample(1:3, 100,replace=TRUE), 1:3,
c('BRCA','LUNG','OV'))
# Cumulative Incidence Function
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
require(cmprsk)
fit3 <- cuminc(ftime = failure_time, fstatus = status,
group = disease)
# Visualize
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ggcompetingrisks(fit3, palette = "Dark2",
legend = "top",
ggtheme = theme_bw())
```

The **ggcometingrisks**() function has also support for **multi-state survival** objects (type = “mstate”), where the status variable can have multiple levels. The first of these will stand for censoring, and the others for various event types, e.g., causes of death.

```
# Data preparation
df <- data.frame(time = failure_time, status = status,
group = disease)
# Fit multi-state survival
library(survival)
fit5 <- survfit(Surv(time, status, type = "mstate") ~ group, data = df)
ggcompetingrisks(fit5, palette = "jco")
```

This analysis has been performed using **R software** (ver. 3.3.2).

I’m very pleased to announce survminer 0.2.4. It comes with many new features and minor changes.

Install survminer with:

`install.packages("survminer")`

To load the package, type this:

`library(survminer)`

- New function
`surv_summary()`

for creating data frame containing a nice summary of survival curves (#64). - It’s possible now to facet the output of
`ggsurvplot()`

by one or more factors (#64). - Now,
`ggsurvplot()`

can be used to plot cox model (#67). - New functions added for determining and visualizing the optimal cutpoint of continuous variables for survival analyses:
`surv_cutpoint()`

: Determine the optimal cutpoint for each variable using ‘maxstat’. Methods defined for surv_cutpoint object are summary(), print() and plot().`surv_categorize()`

: Divide each variable values based on the cutpoint returned by`surv_cutpoint()`

(#41).

- New argument ‘ncensor.plot’ added to
`ggsurvplot()`

. A logical value. If TRUE, the number of censored subjects at time t is plotted. Default is FALSE (#18).

- New argument ‘conf.int.style’ added in
`ggsurvplot()`

for changing the style of confidence interval bands. - Now,
`ggsurvplot()`

plots a stepped confidence interval when conf.int = TRUE (#65). `ggsurvplot()`

updated for compatibility with the future version of ggplot2 (v2.2.0) (#68)- ylab is now automatically adapted according to the value of the argument
`fun`

. For example, if fun = “event”, then ylab will be “Cumulative event”. - In
`ggsurvplot()`

, linetypes can now be adjusted by variables used to fit survival curves (#46) - In
`ggsurvplot()`

, the argument risk.table can be either a logical value (TRUE|FALSE) or a string (“absolute”, “percentage”). If risk.table = “absolute”,`ggsurvplot()`

displays the absolute number of subjects at risk. If risk.table = “percentage”, the percentage at risk is displayed. Use “abs_pct” to show both the absolute number and the percentage of subjects at risk. (#70). - New argument surv.median.line in
`ggsurvplot()`

: character vector for drawing a horizontal/vertical line at median (50%) survival. Allowed values include one of c(“none”, “hv”, “h”, “v”). v: vertical, h:horizontal (#61). - Now, the default theme of ggcoxdiagnostics() is ggplot2::theme_bw().

Compared to the default summary() function, the surv_summary() function [in survminer] creates a data frame containing a nice summary from survfit results.

```
# Fit survival curves
require("survival")
fit <- survfit(Surv(time, status) ~ sex, data = lung)
# Summarize
library("survminer")
res.sum <- surv_summary(fit)
head(res.sum)
```

```
## time n.risk n.event n.censor surv std.err upper lower
## 1 11 138 3 0 0.9782609 0.01268978 1.0000000 0.9542301
## 2 12 135 1 0 0.9710145 0.01470747 0.9994124 0.9434235
## 3 13 134 2 0 0.9565217 0.01814885 0.9911586 0.9230952
## 4 15 132 1 0 0.9492754 0.01967768 0.9866017 0.9133612
## 5 26 131 1 0 0.9420290 0.02111708 0.9818365 0.9038355
## 6 30 130 1 0 0.9347826 0.02248469 0.9768989 0.8944820
## strata sex
## 1 sex=1 1
## 2 sex=1 1
## 3 sex=1 1
## 4 sex=1 1
## 5 sex=1 1
## 6 sex=1 1
```

```
# Information about the survival curves
attr(res.sum, "table")
```

```
## records n.max n.start events *rmean *se(rmean) median 0.95LCL
## sex=1 138 138 138 112 325.0663 22.59845 270 212
## sex=2 90 90 90 53 458.2757 33.78530 426 348
## 0.95UCL
## sex=1 310
## sex=2 550
```

```
ggsurvplot(
fit, # survfit object with calculated statistics.
pval = TRUE, # show p-value of log-rank test.
conf.int = TRUE, # show confidence intervals for
# point estimaes of survival curves.
#conf.int.style = "step", # customize style of confidence intervals
xlab = "Time in days", # customize X axis label.
break.time.by = 200, # break X axis in time intervals by 200.
ggtheme = theme_light(), # customize plot and risk table with a theme.
risk.table = "abs_pct", # absolute number and percentage at risk.
risk.table.y.text.col = T,# colour risk table text annotations.
risk.table.y.text = FALSE,# show bars instead of names in text annotations
# in legend of risk table.
ncensor.plot = TRUE, # plot the number of censored subjects at time t
surv.median.line = "hv", # add the median survival pointer.
legend.labs =
c("Male", "Female"), # change legend labels.
palette =
c("#E7B800", "#2E9FDF") # custom color palettes.
)
```

The survminer package determines the optimal cutpoint for one or multiple continuous variables at once, using the maximally selected rank statistics from the ‘maxstat’ R package. To learn more, read this: M. Kosiński. R-ADDICT November 2016. Determine optimal cutpoints for numerical variables in survival plots.

Here, we’ll use the myeloma data sets [in the survminer package]. It contains survival data and some gene expression data obtained from multiple myeloma patients.

```
# 0. Load some data
data(myeloma)
head(myeloma[, 1:8])
```

```
## molecular_group chr1q21_status treatment event time CCND1
## GSM50986 Cyclin D-1 3 copies TT2 0 69.24 9908.4
## GSM50988 Cyclin D-2 2 copies TT2 0 66.43 16698.8
## GSM50989 MMSET 2 copies TT2 0 66.50 294.5
## GSM50990 MMSET 3 copies TT2 1 42.67 241.9
## GSM50991 MAF
``` TT2 0 65.00 472.6
## GSM50992 Hyperdiploid 2 copies TT2 0 65.20 664.1
## CRIM1 DEPDC1
## GSM50986 420.9 523.5
## GSM50988 52.0 21.1
## GSM50989 617.9 192.9
## GSM50990 11.9 184.7
## GSM50991 38.8 212.0
## GSM50992 16.9 341.6

```
# 1. Determine the optimal cutpoint of variables
res.cut <- surv_cutpoint(myeloma, time = "time", event = "event",
variables = c("DEPDC1", "WHSC1", "CRIM1"))
summary(res.cut)
```

```
## cutpoint statistic
## DEPDC1 279.8 4.275452
## WHSC1 3205.6 3.361330
## CRIM1 82.3 1.968317
```

```
# 2. Plot cutpoint for DEPDC1
# palette = "npg" (nature publishing group), see ?ggpubr::ggpar
plot(res.cut, "DEPDC1", palette = "npg")
```

`## $DEPDC1`

```
# 3. Categorize variables
res.cat <- surv_categorize(res.cut)
head(res.cat)
```

```
## time event DEPDC1 WHSC1 CRIM1
## GSM50986 69.24 0 high low high
## GSM50988 66.43 0 low low low
## GSM50989 66.50 0 low high high
## GSM50990 42.67 1 low high low
## GSM50991 65.00 0 low low low
## GSM50992 65.20 0 high low low
```

```
# 4. Fit survival curves and visualize
library("survival")
fit <- survfit(Surv(time, event) ~DEPDC1, data = res.cat)
ggsurvplot(fit, risk.table = TRUE, conf.int = TRUE)
```

In this section, we’ll compute survival curves using the combination of multiple factors. Next, we’ll factet the output of ggsurvplot() by a combination of factors

- Fit (complex) survival curves using colon data sets

```
require("survival")
fit2 <- survfit( Surv(time, status) ~ sex + rx + adhere,
data = colon )
```

- Visualize the output using survminer

```
ggsurv <- ggsurvplot(fit2, fun = "event", conf.int = TRUE,
risk.table = TRUE, risk.table.col="strata",
ggtheme = theme_bw())
ggsurv
```

- Faceting survival curves. The plot below shows survival curves by the sex variable faceted according to the values of rx & adhere.

```
curv_facet <- ggsurv$plot + facet_grid(rx ~ adhere)
curv_facet
```

- Facetting risk tables: Generate risk table for each facet plot item

```
ggsurv$table + facet_grid(rx ~ adhere, scales = "free")+
theme(legend.position = "none")
```

- Generate risk table for each facet columns

```
tbl_facet <- ggsurv$table + facet_grid(.~ adhere, scales = "free")
tbl_facet + theme(legend.position = "none")
```

```
# Arrange faceted survival curves and risk tables
g2 <- ggplotGrob(curv_facet)
g3 <- ggplotGrob(tbl_facet)
min_ncol <- min(ncol(g2), ncol(g3))
g <- gridExtra::rbind.gtable(g2[, 1:min_ncol], g3[, 1:min_ncol], size="last")
g$widths <- grid::unit.pmax(g2$widths, g3$widths)
grid::grid.newpage()
grid::grid.draw(g)
```

This analysis has been performed using **R software** (ver. 3.3.2).

**Survival analysis** focuses on the expected duration of time until occurrence of an event of interest. However, this failure time may not be observed within the study time period, producing the so-called **censored** observations.

The R package **survival** fits and plots survival curves using R base graphs. There are also several R packages/functions for drawing **survival curves** using ggplot2 system:

These packages/functions are limited:

The default graph generated with the R package

**survival**is ugly and it requires programming skills for drawing a nice looking survival curves. There is no option for displaying the**‘number at risk’**table.**GGally**and**ggfortify**don’t contain any option for drawing the**‘number at risk’**table. You need also some knowledge in**ggplot2**plotting system for drawing a ready-to-publish survival curves.

Here, we developed and present the **survminer** R package for facilitating **survival analysis** and **visualization**.

The current version contains the function **ggsurvplot()** for easily drawing beautiful and ready-to-publish survival curves using **ggplot2**. **ggsurvplot()** includes also some options for displaying the **p-value** and the **‘number at risk’ table**, under the survival curves.

Install from CRAN:

`install.packages("survminer")`

Or, install the latest version from GitHub:

```
# Install
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/survminer")
```

```
# Loading
library("survminer")
```

The R package **survival** is required for fitting survival curves.

```
# Fit survival curves
require("survival")
fit <- survfit(Surv(time, status) ~ 1, data = lung)
# Drawing curves
ggsurvplot(fit, color = "#2E9FDF")
```

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

```
# Change font style, size and color
#++++++++++++++++++++++++++++++++++++
# Change only font size
ggsurvplot(fit, main = "Survival curve",
font.main = 18,
font.x = 16,
font.y = 16,
font.tickslab = 14)
```

```
# 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"))
```

```
# 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))
```

```
# 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"
ggsurvplot(fit, linetype = "strata",
conf.int = TRUE, pval = TRUE,
palette = "Dark2")
```

```
# Use grey palette
ggsurvplot(fit, linetype = "strata",
conf.int = TRUE, pval = TRUE,
palette = "grey")
```

```
# Add risk table
# and change risk table y text colors by strata
ggsurvplot(fit, pval = TRUE, conf.int = TRUE,
risk.table = TRUE, risk.table.y.text.col = TRUE)
```

```
# Customize the output and then print
res <- ggsurvplot(fit, pval = TRUE, conf.int = TRUE,
risk.table = TRUE)
res$table <- res$table + theme(axis.line = element_blank())
res$plot <- res$plot + labs(title = "Survival Curves")
print(res)
```

```
# 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
linetype = "strata", # Change line type by groups
ggtheme = theme_bw(), # Change ggplot2 theme
palette = c("#E7B800", "#2E9FDF"))
```

```
# Change x axis limits (xlim)
#++++++++++++++++++++++++++++++++++++
# One would like to cut axes at a specific time point
ggsurvplot(fit,
pval = TRUE, conf.int = TRUE,
risk.table = TRUE, # Add risk table
risk.table.col = "strata", # Change risk table color by groups
ggtheme = theme_bw(), # Change ggplot2 theme
palette = "Dark2",
xlim = c(0, 600))
```

```
# Plot cumulative events
ggsurvplot(fit, conf.int = TRUE,
palette = c("#FF9E29", "#86AA00"),
risk.table = TRUE, risk.table.col = "strata",
fun = "event")
```

```
# Plot the cumulative hazard function
ggsurvplot(fit, conf.int = TRUE,
palette = c("#FF9E29", "#86AA00"),
risk.table = TRUE, risk.table.col = "strata",
fun = "cumhaz")
```

```
# Arbitrary function
ggsurvplot(fit, conf.int = TRUE,
palette = c("#FF9E29", "#86AA00"),
risk.table = TRUE, risk.table.col = "strata",
pval = TRUE,
fun = function(y) y*100)
```

```
# Fit (complexe) survival curves
#++++++++++++++++++++++++++++++++++++
require("survival")
fit2 <- survfit( Surv(time, status) ~ rx + adhere,
data = colon )
# Visualize
#++++++++++++++++++++++++++++++++++++
# Visualize: add p-value, chang y limits
# change color using brewer palette
ggsurvplot(fit2, pval = TRUE,
break.time.by = 800,
risk.table = TRUE,
risk.table.height = 0.5#Useful when you have multiple groups
)
```

```
# Adjust risk table and survival plot heights
# ++++++++++++++++++++++++++++++++++++
# Risk table height
ggsurvplot(fit2, pval = TRUE,
break.time.by = 800,
risk.table = TRUE,
risk.table.col = "strata",
risk.table.height = 0.5,
palette = "Dark2")
```

```
# Change legend labels
# ++++++++++++++++++++++++++++++++++++
ggsurvplot(fit2, pval = TRUE,
break.time.by = 800,
risk.table = TRUE,
risk.table.col = "strata",
risk.table.height = 0.5,
ggtheme = theme_bw(),
legend.labs = c("A", "B", "C", "D", "E", "F"))
```

This article was built with:

```
## setting value
## version R version 3.2.3 (2015-12-10)
## system x86_64, darwin13.4.0
## ui X11
## language (EN)
## collate fr_FR.UTF-8
## tz Europe/Paris
## date 2016-02-21
##
## package * version date source
## colorspace 1.2-6 2015-03-11 CRAN (R 3.2.0)
## dichromat 2.0-0 2013-01-24 CRAN (R 3.2.0)
## digest 0.6.9 2016-01-08 CRAN (R 3.2.3)
## ggplot2 * 2.0.0 2015-12-18 CRAN (R 3.2.3)
## gridExtra 2.0.0 2015-07-14 CRAN (R 3.2.0)
## gtable 0.1.2 2012-12-05 CRAN (R 3.2.0)
## labeling 0.3 2014-08-23 CRAN (R 3.2.0)
## magrittr 1.5 2014-11-22 CRAN (R 3.2.0)
## MASS 7.3-45 2015-11-10 CRAN (R 3.2.3)
## munsell 0.4.3 2016-02-13 CRAN (R 3.2.3)
## plyr 1.8.3 2015-06-12 CRAN (R 3.2.0)
## RColorBrewer 1.1-2 2014-12-07 CRAN (R 3.2.0)
## Rcpp 0.12.3 2016-01-10 CRAN (R 3.2.3)
## reshape2 1.4.1 2014-12-06 CRAN (R 3.2.0)
## scales 0.3.0 2015-08-25 CRAN (R 3.2.0)
## stringi 1.0-1 2015-10-22 CRAN (R 3.2.0)
## stringr 1.0.0 2015-04-30 CRAN (R 3.2.0)
## survival * 2.38-3 2015-07-02 CRAN (R 3.2.3)
## survminer * 0.2.0 2016-02-18 CRAN (R 3.2.3)
```