#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# 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
linetype = "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)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Example 4: Facet ggsurvplot() output by
# a combination of factors
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Fit (complexe) survival curves
#++++++++++++++++++++++++++++++++++++
## Not run:
# require("survival")
# fit3 <- survfit( Surv(time, status) ~ sex + rx + adhere,
# data = colon )
#
# # Visualize
# #++++++++++++++++++++++++++++++++++++
# ggsurv <- ggsurvplot(fit3, fun = "cumhaz", conf.int = TRUE,
# risk.table = TRUE, risk.table.col="strata",
# ggtheme = theme_bw())
#
# # Faceting survival curves
# curv_facet <- ggsurv$plot + facet_grid(rx ~ adhere)
# curv_facet
#
# # Faceting 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)
#
#
#
#
# ## End(Not run)
Run the code above in your browser using DataLab