R语言可视化(三十七):生存曲线图绘制
2020-12-05 本文已影响0人
Davey1220
37. 生存曲线图绘制
清除当前环境中的变量
rm(list=ls())
设置工作目录
setwd("C:/Users/Dell/Desktop/R_Plots/37survival/")
使用survival包进行生存分析
# 安装并加载所需的R包
#install.packages("survival") # 安装survival包
library(survival) # 加载包
#查看内置数据集
head(aml)
## time status x
## 1 9 1 Maintained
## 2 13 1 Maintained
## 3 13 0 Maintained
## 4 18 1 Maintained
## 5 23 1 Maintained
## 6 28 0 Maintained
#time # 生存时间,天数;
#status # 生存状态,0为截尾(删失值),1为死亡;
#x # 分组变量,Maintained和Nonmaintained
# 构建生存对象
Surv(aml$time, aml$status)
## [1] 9 13 13+ 18 23 28+ 31 34 45+ 48 161+ 5 5 8
## [15] 8 12 16+ 23 27 30 33 43 45
# 使用survfit()函数来拟合Kaplan-Meier生存曲线
fit <- survfit(Surv(time, status) ~ x, data = aml)
# 查看生存曲线拟合结果
fit
## Call: survfit(formula = Surv(time, status) ~ x, data = aml)
##
## n events median 0.95LCL 0.95UCL
## x=Maintained 11 7 31 18 NA
## x=Nonmaintained 12 11 23 8 NA
summary(fit)
## Call: survfit(formula = Surv(time, status) ~ x, data = aml)
##
## x=Maintained
## time n.risk n.event survival std.err lower 95% CI upper 95% CI
## 9 11 1 0.909 0.0867 0.7541 1.000
## 13 10 1 0.818 0.1163 0.6192 1.000
## 18 8 1 0.716 0.1397 0.4884 1.000
## 23 7 1 0.614 0.1526 0.3769 0.999
## 31 5 1 0.491 0.1642 0.2549 0.946
## 34 4 1 0.368 0.1627 0.1549 0.875
## 48 2 1 0.184 0.1535 0.0359 0.944
##
## x=Nonmaintained
## time n.risk n.event survival std.err lower 95% CI upper 95% CI
## 5 12 2 0.8333 0.1076 0.6470 1.000
## 8 10 2 0.6667 0.1361 0.4468 0.995
## 12 8 1 0.5833 0.1423 0.3616 0.941
## 23 6 1 0.4861 0.1481 0.2675 0.883
## 27 5 1 0.3889 0.1470 0.1854 0.816
## 30 4 1 0.2917 0.1387 0.1148 0.741
## 33 3 1 0.1944 0.1219 0.0569 0.664
## 43 2 1 0.0972 0.0919 0.0153 0.620
## 45 1 1 0.0000 NaN NA NA
# 绘制基础KM生存曲线
plot(fit,xlab="Time(Days)",ylab="Survival probability",
col=c("blue","red"),lty=2:3,lwd=2)
# 添加图例
legend("topright",c("Maintained","Nonmaintained"),
col=c("blue","red"),lty=2:3,lwd=2,cex=1)
image.png
使用survminer包绘制生存曲线
# 安装并加载所需的R包
#install.packages("survminer") # 安装survminer包
library(survminer) # 加载包
## Warning: package 'survminer' was built under R version 3.6.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.3
## Loading required package: ggpubr
## Loading required package: magrittr
# 查看内置数据集
head(lung)
## inst time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss
## 1 3 306 2 74 1 1 90 100 1175 NA
## 2 3 455 2 68 1 0 90 90 1225 15
## 3 3 1010 1 56 1 0 90 90 NA 15
## 4 5 210 2 57 1 1 90 60 1150 11
## 5 1 883 2 60 1 0 100 90 NA 0
## 6 12 1022 1 74 1 1 50 80 513 0
#time # 生存时间,天数;
#status # 生存状态,1为截尾数据,2为死亡数据;
#age # 年龄;
#sex # 分组数据,性别:1为男性,2为女性;
# 使用survfit()函数拟合KM生存曲线
fit <- survfit(Surv(time, status) ~ sex, data = lung)
# 使用ggsurvplot()函数绘制基础KM生存曲线
ggsurvplot(fit, data = lung)
image.png
# Change font size, style and color
ggsurvplot(fit, data = lung,
main = "Survival curve", # 添加标题
font.main = c(16, "bold", "darkblue"), # 设置标题字体大小、格式和颜色
font.x = c(14, "bold.italic", "red"), # 设置x轴字体大小、格式和颜色
font.y = c(14, "bold.italic", "darkred"), # 设置y轴字体大小、格式和颜色
font.tickslab = c(12, "plain", "darkgreen")) # 设置坐标轴刻度字体大小、格式和颜色
image.png
# Customized survival curves
ggsurvplot(fit, data = lung,
surv.median.line = "hv", # 添加中位数生存时间线
# Change legends: title & labels
legend.title = "Sex", # 设置图例标题
legend.labs = c("Male", "Female"), # 指定图例分组标签
# Add p-value and tervals
pval = TRUE, # 设置添加P值
pval.method = TRUE, #设置添加P值计算方法
conf.int = TRUE, # 设置添加置信区间
# Add risk table
risk.table = TRUE, # 设置添加风险因子表
tables.height = 0.2, # 设置风险表的高度
tables.theme = theme_cleantable(), # 设置风险表的主题
# Color palettes. Use custom color: c("#E7B800", "#2E9FDF"),
# or brewer color (e.g.: "Dark2"), or ggsci color (e.g.: "jco")
palette = c("#E7B800", "#2E9FDF"), # 设置颜色画板
ggtheme = theme_bw() # Change ggplot2 theme
)
image.png
# 绘制分面生存曲线
# 查看示例数据
head(colon)
## id study rx sex age obstruct perfor adhere nodes status differ
## 1 1 1 Lev+5FU 1 43 0 0 0 5 1 2
## 2 1 1 Lev+5FU 1 43 0 0 0 5 1 2
## 3 2 1 Lev+5FU 1 63 0 0 0 1 0 2
## 4 2 1 Lev+5FU 1 63 0 0 0 1 0 2
## 5 3 1 Obs 0 71 0 0 1 7 1 2
## 6 3 1 Obs 0 71 0 0 1 7 1 2
## extent surg node4 time etype
## 1 3 0 1 1521 2
## 2 3 0 1 968 1
## 3 3 0 0 3087 2
## 4 3 0 0 3087 1
## 5 2 0 1 963 2
## 6 2 0 1 542 1
# 拟合KM生存曲线
fit <- survfit( Surv(time, status) ~ sex, data = colon)
# 使用ggsurvplot_facet()函数绘制分面生存曲线
ggsurvplot_facet(fit, colon,
facet.by = "rx", # 设置分面变量
palette = "jco", # 设置颜色画板
pval = TRUE) # 添加pvalue值
image.png
# Facet by two grouping variables: rx and adhere
ggsurvplot_facet(fit, colon,
facet.by = c("rx", "adhere"),
palette = "npg",
pval = TRUE,
surv.median.line = "hv", # 增加中位生存时间
conf.int = TRUE) # 增加置信区间)
image.png
# 拟合多个分组变量
fit2 <- survfit( Surv(time, status) ~ sex + rx, data = colon )
fit2
ggsurvplot_facet(fit2, colon,
facet.by = "adhere",
palette = "lancet",
pval = TRUE,
pval.method = TRUE,
surv.median.line = "hv")
image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] survminer_0.4.8 ggpubr_0.2.1 magrittr_1.5 ggplot2_3.3.2
## [5] survival_2.44-1.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 pillar_1.4.2 compiler_3.6.0
## [4] tools_3.6.0 digest_0.6.20 nlme_3.1-139
## [7] evaluate_0.14 tibble_2.1.3 lifecycle_0.2.0
## [10] gtable_0.3.0 lattice_0.20-38 pkgconfig_2.0.2
## [13] rlang_0.4.7 Matrix_1.2-17 ggsci_2.9
## [16] yaml_2.2.0 xfun_0.8 gridExtra_2.3
## [19] withr_2.1.2 stringr_1.4.0 dplyr_1.0.2
## [22] knitr_1.23 survMisc_0.5.5 generics_0.0.2
## [25] vctrs_0.3.2 grid_3.6.0 tidyselect_1.1.0
## [28] data.table_1.12.2 glue_1.4.2 KMsurv_0.1-5
## [31] R6_2.4.0 km.ci_0.5-2 rmarkdown_1.13
## [34] tidyr_1.1.2 purrr_0.3.2 ellipsis_0.2.0.1
## [37] backports_1.1.4 scales_1.0.0 htmltools_0.3.6
## [40] splines_3.6.0 xtable_1.8-4 colorspace_1.4-1
## [43] ggsignif_0.5.0 labeling_0.3 stringi_1.4.3
## [46] munsell_0.5.0 broom_0.5.2 crayon_1.3.4
## [49] zoo_1.8-6