根据是上一篇survminer包使用教程[1],结合实验数据,实际应用绘制生存曲线。
实验数据:
从肠道中分离出三种共生菌,分别喂食给昆虫,无菌水作对照,每日统计死亡情况,直至个体全部死亡,并绘图。
原始数据如下所示:
其中,time是生存时间,1表示死亡,NA表示存活(或空白)。[2]
如果在相同时间段有好几个死亡事件,则重复输入即可,例如第5行到第6行,都是死亡事件,则连续输入2次。
1. 清理数据
rm(list = ls())
# 加载R包
library("survminer")
dorsalis <- read.table(file = "C:/Users/Administrator/Desktop/surval.txt", header = TRUE, sep = "")
head(dorsalis)
id time Water Eubacteria Acetobacter Klebsiella
1 1 8 NA NA NA 1
2 2 9 NA NA NA 1
3 3 19 NA NA 1 NA
4 4 20 NA 1 NA NA
5 5 23 1 NA NA 1
6 6 23 1 NA NA NA
# 长-宽型数据的相互转换
library(reshape2)
melted_dorsalis <- melt(dorsalis, id.vars = c("id","time"), variable.name = "treated",
measure.vars=c("Water","Eubacteria", "Acetobacter", "Klebsiella"))
head(melted_dorsalis)
id time treated value
1 1 8 Water NA
2 2 9 Water NA
3 3 19 Water NA
4 4 20 Water NA
5 5 23 Water 1
6 6 23 Water 1
2. 拟合生存曲线并绘图
# 拟合曲线
fit <- survfit(Surv(time, value) ~ treated, data = melted_dorsalis)
fit
Call: survfit(formula = Surv(time, value) ~ treated, data = melted_dorsalis)
499 observations deleted due to missingness
n events median 0.95LCL 0.95UCL
treated=Water 40 40 117 99 131
treated=Eubacteria 71 71 145 121 167
treated=Acetobacter 70 70 146 121 175
treated=Klebsiella 40 40 136 91 161
# 绘图
ggsurvplot(fit)
# 添加风险表
ggsurvplot(fit, pval = TRUE, conf.int = TRUE,
risk.table = TRUE, risk.table.y.text.col = TRUE)
- Rplot01.png
-
Rplot02.png
最终效果:
ggsurvplot(fit, pval = TRUE,
risk.table = TRUE,
risk.table.col = "strata",
legend.title = "Treated",
title = "Survival curve",
legend.labs = c("Water", "Eubacteria", "Acetobacter", "Klebsiella"),
fun = function(y) y*100)
-
Rplot03.png
参考资料:
网友评论