survminer包是生物資訊學中實現生存分析曲線繪製最常用的包,包含函數ggsurvplot(),還可使用ggplot2. ggsurvplot() 輕鬆繪製美觀並且格式工整的生存曲線。同時還能給出p值,風險值等參數。
install.packages("survminer")
或者,也可以選擇從GitHub上安裝
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/survminer")
載入安裝好的包
library("survminer")
#呼叫生存曲線中另一常用的survival包
require("survival")
#survival包自帶肺癌數據集:lung,檢視數據樣式
head(lung)
#survival包中的Sruv函數可以建立一個生存物件
fit <- survfit(Surv(time, status) ~ 1, data = lung)
#survival包中的survfit函數用Kaplan-Meier法進行生存曲線的擬合
ggsurvplot(fit, color = "#2E9FDF")
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit)
title = 「Survival curve」 | 設定標題 |
---|---|
font.main | 標題字型大小 |
font.x | x軸標註字型大小 |
font.y | y軸標註字型大小 |
font.tickslab | 座標軸數位大小 |
ggsurvplot(fit, title = "Survival curve",
font.title = 18,
font.x = 16,
font.y = 16,
font.tickslab = 14)
ggsurvplot(fit, title = "Survival curve",
font.title = c(16, "bold", "darkblue"),
font.x = c(14, "bold.italic", "red"),
font.y = c(14, "bold.italic", "darkred"),
font.tickslab = c(12, "plain", "darkgreen"))
ggsurvplot(fit, legend = "bottom",
legend.title = "Sex",
legend.labs = c("Male", "Female"))
ggsurvplot(fit, legend = c(0.2, 0.2))
ggsurvplot(fit, size = 1, # 改變曲線的尺寸
linetype = "strata", # 改變曲線的型別
break.time.by = 250, # 更改x軸的時間間隔
palette = c("#E7B800", "#2E9FDF"), # 自定義調色板
conf.int = TRUE, # 新增置信區間
pval = TRUE # 新增p值
)
ggsurvplot(fit, linetype = "strata",
conf.int = TRUE, pval = TRUE,
palette = "grey")
ggsurvplot(fit, pval = TRUE, conf.int = TRUE,
risk.table = TRUE, risk.table.y.text.col = TRUE)
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)
ggsurvplot(fit,
pval = TRUE, conf.int = TRUE,
risk.table = TRUE,
risk.table.col = "strata",
ggtheme = theme_bw(), #改變ggplot2的主題(背景)
palette = "Dark2",
xlim = c(0, 600))#選取特定的x軸座標範圍
ggsurvplot(fit, conf.int = TRUE,
palette = c("#FF9E29", "#86AA00"),
risk.table = TRUE, risk.table.col = "strata",
fun = "event")
ggsurvplot(fit, conf.int = TRUE,
palette = c("#FF9E29", "#86AA00"),
risk.table = TRUE, risk.table.col = "strata",
fun = "cumhaz")
ggsurvplot(fit, conf.int = TRUE,
palette = c("#FF9E29", "#86AA00"),
risk.table = TRUE, risk.table.col = "strata",
pval = TRUE,
fun = function(y) y*100)
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"))