1. 导入包

绘制分组Kaplan-Meier生存曲线需要用到的R包:survminersurvival ,如果没有请安装。

    install.packages("survminer")  #下载survminer包
    install.packages("survival")   #下载survival包
    library(survminer)  #加载包
    library(survival)  #加载包

2. 导入数据集

    data(colon)  # 加载数据集
    View(colon)  # 预览数据集

1.png

2.1 数据集介绍

    ?colon # 查询 colon 数据集信息

2.png

2.2 数据部分变量介绍

colon数据集:B/C期结肠癌辅助化疗治疗数据

id:患者编号
study: 所有患者都是1
rx:治疗方式,有三种:观察、Levamisole、Levamisole+5-FU
sex: 性别,男性为 1,女性为 0
age: 年龄
obstruct:肿瘤是否阻塞结肠,有为1,无为0
perfor:    结肠是否穿孔,有为1,无为0
adhere:肿瘤是否粘附附近器官,有为1,无为0
nodes: 检出淋巴结的数目
time:至发生终点事件或删失的时间
status:    生存状态,1为发生终点事件,0为删失
differ:    肿瘤分化程度 (1=well, 2=moderate, 3=poor)
extent:    局部转移程度(1=粘膜下层,2=肌肉,3=浆膜,4=相邻结构)
surg:从手术到登记的时间 (0=short, 1=long)
node4: 超过4个阳性淋巴结,有为1,无为0
etype: 事件类型: 1=复发,2=死亡

3. 拟合曲线

    fit <- survfit( Surv(time, status) ~ sex, data = colon ) # 拟合性别 sex 的生存曲线

3.1 1个分组变量

3.1.1 简单绘制

    ggsurvplot_facet(fit, colon, facet.by = "obstruct",
                     palette = "lancet", pval = TRUE)  # 以 obstruct 分组

3.png

    ggsurvplot_facet(fit, colon, facet.by = "differ",
                     palette = "lancet", pval = TRUE)  # 以 differ 分组

4.png

    ggsurvplot_facet(fit, colon, facet.by = "extent",
                     palette = "lancet", pval = TRUE)  # 以 extent 分组

5.png

3.1.2 美化曲线

    ggsurvplot_facet(fit,  
                     data = colon,  
                     facet.by = "differ",  # 以 differ 分组
                     palette = "lancet", 
                     legend.labs = c("Male","Female"), 
                     legend = c(0.9,0.8),
                     legend.title = "",
                     pval = TRUE, 
                     pval.method = TRUE, 
                     pval.coord = c(1500,0.2), 
                     pval.method.coord = c(600,0.2), 
                     nrow = 1, ncol = 3, 
                     panel.labs = list(differ = c("well","moderate","poor")), 
                     panel.labs.background = list(color = "blue", fill = "pink"), 
                     panel.labs.font = list(face = "plain", color = "black", size = 14))

6.png

3.2 2个分组变量

3.2.1 简单绘制

    ggsurvplot_facet(fit, colon, facet.by = c("obstruct", "adhere"),
                     palette = "lancet", pval = TRUE) # 以"obstruct", "adhere"为例

7.png

    ggsurvplot_facet(fit, colon, facet.by = c("obstruct", "rx"),
                     palette = "lancet", pval = TRUE) # 以"obstruct", "rx"为例

8.png

    ggsurvplot_facet(fit, colon, facet.by = c("obstruct", "extent"),
                     palette = "lancet", pval = TRUE) # 以"obstruct", "extent"为例

9.png

    ggsurvplot_facet(fit, colon, facet.by = c("rx", "extent"),
                     palette = "lancet", pval = TRUE) # 以"rx", "extent"为例

10.png

3.2.2 美化曲线

    ggsurvplot_facet(fit,  
                     data = colon,  
                     facet.by = c("obstruct", "adhere"),  
                     palette = "lancet", 
                     legend.labs = c("Male","Female"), 
                     legend = c(0.9,0.9),
                     legend.title = "",
                     pval = TRUE, 
                     pval.method = TRUE, 
                     pval.coord = c(1500,0.2), 
                     pval.method.coord = c(1000,0.2), 
                     nrow = 1, ncol = 3, 
                     panel.labs = list(obstruct = c("No", "YES"), adhere = c("No", "YES")), 
                     panel.labs.background = list(color = "blue", fill = "pink"), 
                     panel.labs.font = list(face = "plain", color = "black", size = 14))

11.png


4. 其他拟合曲线

    fit2 <- survfit( Surv(time, status) ~ sex + differ, data = colon )

4.1 简单绘制

    ggsurvplot_facet(fit2, colon, facet.by = "adhere",
                     palette = "lancet", pval = TRUE)

12.png

4.2 美化曲线

    ggsurvplot_facet(fit2,  
                     data = colon,  
                     facet.by = "adhere",  
                     palette = "lancet", 
                     legend.labs = c("Female & Well","Female & moderate","Female & Poor","Male & Well","Male & moderate", "Male & Poor"),
                     legend = c(0.9,0.8),
                     legend.title = "",
                     pval = TRUE, 
                     pval.method = TRUE, 
                     pval.coord = c(1500,0.2), 
                     pval.method.coord = c(1000,0.2), 
                     nrow = 1, ncol = 3, 
                     panel.labs = list(adhere = c("No", "YES")), 
                     panel.labs.background = list(color = "blue", fill = "pink"), 
                     panel.labs.font = list(face = "plain", color = "black", size = 14))

13.png


5. ggsurvplot_facet()函数

    ggsurvplot_facet(fit,  # 拟合的生存对象
                     data,  # 拟合生存曲线的数据集
                     facet.by,  # 长度为1或2的字符向量,分组生存曲线中指定的数据集中的分组变量
                     color = NULL,  # 生存曲线的颜色, n.strata = 1, 则color = "blue"; n.strata>1,默认 color = "strata",按分组为生存曲线着色
                     palette = NULL, # 自定义调色板设置颜色, 自定义颜色:c("blue", "red");或brewer palettes如"RdBu", "Blues";或scientific journal palettes如 "npg", "aaas", "lancet", "jco", "ucscgb", "uchicago", "simpsons" and "rickandmorty"
                     legend.labs = NULL, # 指定图例标签的字符向量,用来替换拟fit中strata的名称,顺序应该和strata一样
                     pval = FALSE, # P值添加有3种方式,逻辑词、数值或字符串;为TRUE则图上添加计算的p值;为数字则将此数值代替计算的p值;为字符串则将自定义的p值添加到图上。
                     pval.method = FALSE, # 仅在pval=TRUE时有效,添加计算p值的检验名称的文本,该文本对应生存曲线的比较
                     pval.coord = NULL, # 长度为2的向量,指定p值的坐标,如c(x,y)
                     pval.method.coord = NULL, # 同上,指定检验方法文本的坐标
                     nrow = NULL, # 面板中的行数和列数,仅当一个分组变量时使用
                     ncol = NULL, 
                     scales = "fixed", # 面板中坐标轴比例,默认"fixed",其他还有"free", "free_x", "free_y"
                     short.panel.labs = FALSE, # 逻辑词,默认为FALSE,为TRUE,则省略变量名称为面板创建简短标签
                     panel.labs = NULL, # 1个或2个字符向量的列表,用于修改标签文本。如panel.labs = list(sex = c(“ Male”,“ Female”))指定“ sex”变量的标签。两个分组变量,如panel.labs = list(sex = c(“ Male”,“ Female”),rx = c(“ Obs”,“ Lev”,“ Lev2”)))。
                     panel.labs.background = list(color = NULL, fill =NULL), # 自定义面板标签背景的列表,应包含以下元素(color, linetype, size: 背景线的颜色、类型和大小;fill:背景填充颜色),如panel.labs.background = list(color = "blue", fill = "pink").
                     panel.labs.font = list(face = NULL, color = NULL, size = NULL,angle = NULL), # 指定面板标签的大小,外观/样式,颜色和方向角度。
                     panel.labs.font.x = panel.labs.font,  # 同上,当分别指定x轴和y轴
                     panel.labs.font.y = panel.labs.font, ...) # 其他参数同ggsurvplot()函数

5. 感谢

SOURCE:R语言统计与绘图