美文网首页R学习与可视化
R绘图_survminer绘制生存曲线

R绘图_survminer绘制生存曲线

作者: 谢俊飞 | 来源:发表于2020-03-05 09:33 被阅读0次
  • STHDA Web

survminer R package: Survival Data Analysis and Visualization

survminer R包:生存数据分析和可视化

生存分析指的是一系列用来探究所感兴趣的事件的发生的时间的统计方法。[1]
survminer无疑是一款优秀的生存分析包,可以同时绘制生存曲线和风险数字表,操作简单,出图快速且美观。

1. 绘制无分组生存曲线
rm(list = ls())
install.packages("survminer")

# 加载R包
library("survminer")
#绘制无分组生存曲线
require("survival")
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
# 拟合生存曲线
fit1 <- survfit(Surv(time, status) ~ 1, data = lung)
# 绘制生存曲线
ggsurvplot(fit1, color = "#2E9FDF")
  • Rplot01.png

2. 绘制两组的生存曲线

# 绘制两组的生存曲线
fit2 <- survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit2 )
  • Rplot02.png
# 更改字体大小、风格及颜色
ggsurvplot(fit2, title = "Survival curve",
           font.main = 18,     #标题字体大小
           font.x = 16,        #x坐标字体大小
           font.y = 16,        #y坐标字体大小
           font.tickslab = 14) #轴须字体
# 同时更改字体大小、风格及颜色
ggsurvplot(fit2, title = "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"))
  • Rplot04.png
# 更改图例位置、标题及标签
ggsurvplot(fit2, legend = "bottom", 
           legend.title = "Sex",
           legend.labs = c("Male", "Female"))
# 坐标上指定图例位置
ggsurvplot(fit2, legend = c(0.2, 0.2))
  • Rplot05.png
  • Rplot06.png
# 更改线类型和色板
ggsurvplot(fit2,  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
)

还有两种配色风格 palette = "Dark2" 、palette = "grey"

  • Rplot07.png
  • Rplot08.png
# 在风险表中添加数字
ggsurvplot(fit2, pval = TRUE, conf.int = TRUE,
           risk.table = TRUE, risk.table.y.text.col = TRUE)

# 自定义输出
res <- ggsurvplot(fit2, 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)
  • Rplot09.png
  • Rplot10.png
ggsurvplot(fit2,
           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")
           )

  • Rplot11.png
# 更改x轴范围
xlim = c(0, 600)
# 转换生存曲线:绘制累积事件和危害函数图
ggsurvplot(fit2, conf.int = TRUE,
           palette = c("#FF9E29", "#86AA00"),
           risk.table = TRUE, risk.table.col = "strata",
           fun = "event")          #转换生存曲线
# 绘制累积危害函数
fun = "cumhaz"
# 任意函数
fun = function(y) y*100
  • Rplot12.png

2. 绘制多组的生存曲线

# 多组生存曲线
head(colon)
id study      rx sex age obstruct perfor adhere nodes status differ extent surg node4 time etype
1  1     1 Lev+5FU   1  43        0      0      0     5      1      2      3    0     1 1521     2
2  1     1 Lev+5FU   1  43        0      0      0     5      1      2      3    0     1  968     1
3  2     1 Lev+5FU   1  63        0      0      0     1      0      2      3    0     0 3087     2
4  2     1 Lev+5FU   1  63        0      0      0     1      0      2      3    0     0 3087     1
5  3     1     Obs   0  71        0      0      1     7      1      2      2    0     1  963     2
6  3     1     Obs   0  71        0      0      1     7      1      2      2    0     1  542     1
fit3 <- survfit( Surv(time, status) ~ rx + adhere,
                 data = colon )
ggsurvplot(fit3, pval = TRUE,
           break.time.by = 800,
           risk.table = TRUE,
           risk.table.height = 0.5)   # 指定风险表高度(当组分多时非常有用)
  • Rplot13.png
# 更改图例标签
ggsurvplot(fit3, 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"))
  • Rplot14.png

  1. 生存分析(survival analysis)

相关文章

网友评论

    本文标题:R绘图_survminer绘制生存曲线

    本文链接:https://www.haomeiwen.com/subject/hklglhtx.html