#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# 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)
Run the code above in your browser using DataLab