从坐标重建生存曲线
Reconstruct survival curve from coordinates
下面是我使用 survival
包从 survfit 对象中提取的数据集。
> km_data <- data.table(time = c(72, 109, 143),
n.risk = c(80, 77, 76),
n.event = c(1, 1, 2),
n.censor = c(3, 2, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974),
strata = c(0, 0, 0),
type = "right")
我需要使用上面的 table km_data
重建 KM 曲线(而不是直接从 survfit 对象绘制),但现在确定如何。关于如何在 R 中执行此操作的任何想法,最好使用 ggplot2
?谢谢!
首先,我将添加一行,在时间 0 和生存概率 1 处启动 KM(此步骤是可选的)。然后只需使用 geom_step()
制作按层分组的折线图。
library(ggplot2)
library(dplyr)
km_data <- data.frame(time = c(72, 109, 143, 153, 172, 180, 231, 288, 321, 341, 387, 392, 445, 459, 470, 491, 503, 528, 537, 557, 593, 638, 727, 861, 868, 891, 904, 983, 1011, 1160, 1195, 1266, 1276, 1333, 2176, 33, 37, 98, 106, 167, 231, 257, 278, 296, 306, 331, 345, 350, 524, 570, 594, 607, 622, 625, 668, 671, 740, 835, 862, 898, 1056, 1119, 1259, 1267, 1277, 1319, 1339, 1374),
n.risk = c(80, 77, 76, 74, 73, 72, 69, 66, 65, 64, 63, 62, 57, 56, 55, 53, 52, 51, 50, 47, 46, 45, 41, 38, 37, 36, 35, 34, 33, 28, 24, 23, 22, 20, 1, 85, 84, 83, 81, 80, 76, 74, 73, 72, 70, 69, 68, 67, 63, 60, 58, 57, 56, 54, 52, 51, 48, 46, 44, 43, 40, 36, 31, 30, 29, 26, 25, 24),
n.event = c(1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
n.censor = c(3, 2, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 4, 0, 0, 1, 0, 0, 0, 2, 0, 0, 3, 2, 0, 0, 0, 0, 0, 4, 3, 0, 0, 1, 18, 0, 0, 0, 1, 0, 3, 1, 0, 0, 1, 0, 0, 0, 3, 2, 1, 0, 0, 1, 1, 0, 2, 1, 1, 0, 2, 3, 4, 0, 0, 2, 0, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974, 0.936201298701299, 0.923376623376623, 0.910551948051948, 0.897355543007717, 0.88375924690154, 0.870162950795362, 0.856566654689184, 0.842970358583007, 0.829374062476829, 0.814823640328113, 0.800273218179397, 0.78572279603068, 0.770897837615007, 0.756072879199334, 0.741247920783661, 0.726422962367987, 0.71096715465803, 0.695511346948073, 0.680055539238116, 0.663468818768893, 0.646009113011817, 0.628549407254741, 0.611089701497665, 0.593629995740589, 0.576170289983513, 0.558710584226437, 0.538756634789778, 0.516308441673538, 0.493860248557297, 0.471412055441056, 0.447841452669003, 0, 0.988235294117647, 0.976470588235294, 0.964705882352941, 0.95279593318809, 0.940885984023239, 0.928505905286091, 0.91595852818763, 0.903411151089169, 0.890863773990709, 0.878137148647984, 0.86541052330526, 0.852683897962536, 0.839957272619811, 0.826624617498862, 0.812847540540547, 0.798832927772607, 0.784818315004666, 0.770803702236726, 0.756529559602713, 0.741980914225737, 0.727432268848762, 0.712277429914413, 0.696793137959752, 0.680956930278848, 0.665120722597945, 0.648492704532996, 0.630479018295968, 0.610140985447711, 0.589802952599454, 0.569464919751197, 0.54756242283769, 0.525659925924182, 0.503757429010675),
strata = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
type = "right")
km_data <- bind_rows(km_data, data.frame(strata = c(0,1),surv = c(1,1),
time = c(0,0)))
ggplot(km_data) +
geom_step(aes(x = time, y = surv, group = strata))
使用包 survminer
的 ggsurvplot
功能,它建立在 ggplot2
之上,可用于创建 Kaplan-Meier 图。
library(survminer)
km_data %>% ggsurvplot()
更新 1
新数据更新
km_data <- data.table(time = c(72, 109, 143),
n.risk = c(80, 77, 76),
n.event = c(1, 1, 2),
n.censor = c(3, 2, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974),
strata = c(0, 0, 0),
type = "right")
library(survminer)
km_data %>% ggsurvplot()
下面是我使用 survival
包从 survfit 对象中提取的数据集。
> km_data <- data.table(time = c(72, 109, 143),
n.risk = c(80, 77, 76),
n.event = c(1, 1, 2),
n.censor = c(3, 2, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974),
strata = c(0, 0, 0),
type = "right")
我需要使用上面的 table km_data
重建 KM 曲线(而不是直接从 survfit 对象绘制),但现在确定如何。关于如何在 R 中执行此操作的任何想法,最好使用 ggplot2
?谢谢!
首先,我将添加一行,在时间 0 和生存概率 1 处启动 KM(此步骤是可选的)。然后只需使用 geom_step()
制作按层分组的折线图。
library(ggplot2)
library(dplyr)
km_data <- data.frame(time = c(72, 109, 143, 153, 172, 180, 231, 288, 321, 341, 387, 392, 445, 459, 470, 491, 503, 528, 537, 557, 593, 638, 727, 861, 868, 891, 904, 983, 1011, 1160, 1195, 1266, 1276, 1333, 2176, 33, 37, 98, 106, 167, 231, 257, 278, 296, 306, 331, 345, 350, 524, 570, 594, 607, 622, 625, 668, 671, 740, 835, 862, 898, 1056, 1119, 1259, 1267, 1277, 1319, 1339, 1374),
n.risk = c(80, 77, 76, 74, 73, 72, 69, 66, 65, 64, 63, 62, 57, 56, 55, 53, 52, 51, 50, 47, 46, 45, 41, 38, 37, 36, 35, 34, 33, 28, 24, 23, 22, 20, 1, 85, 84, 83, 81, 80, 76, 74, 73, 72, 70, 69, 68, 67, 63, 60, 58, 57, 56, 54, 52, 51, 48, 46, 44, 43, 40, 36, 31, 30, 29, 26, 25, 24),
n.event = c(1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
n.censor = c(3, 2, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 4, 0, 0, 1, 0, 0, 0, 2, 0, 0, 3, 2, 0, 0, 0, 0, 0, 4, 3, 0, 0, 1, 18, 0, 0, 0, 1, 0, 3, 1, 0, 0, 1, 0, 0, 0, 3, 2, 1, 0, 0, 1, 1, 0, 2, 1, 1, 0, 2, 3, 4, 0, 0, 2, 0, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974, 0.936201298701299, 0.923376623376623, 0.910551948051948, 0.897355543007717, 0.88375924690154, 0.870162950795362, 0.856566654689184, 0.842970358583007, 0.829374062476829, 0.814823640328113, 0.800273218179397, 0.78572279603068, 0.770897837615007, 0.756072879199334, 0.741247920783661, 0.726422962367987, 0.71096715465803, 0.695511346948073, 0.680055539238116, 0.663468818768893, 0.646009113011817, 0.628549407254741, 0.611089701497665, 0.593629995740589, 0.576170289983513, 0.558710584226437, 0.538756634789778, 0.516308441673538, 0.493860248557297, 0.471412055441056, 0.447841452669003, 0, 0.988235294117647, 0.976470588235294, 0.964705882352941, 0.95279593318809, 0.940885984023239, 0.928505905286091, 0.91595852818763, 0.903411151089169, 0.890863773990709, 0.878137148647984, 0.86541052330526, 0.852683897962536, 0.839957272619811, 0.826624617498862, 0.812847540540547, 0.798832927772607, 0.784818315004666, 0.770803702236726, 0.756529559602713, 0.741980914225737, 0.727432268848762, 0.712277429914413, 0.696793137959752, 0.680956930278848, 0.665120722597945, 0.648492704532996, 0.630479018295968, 0.610140985447711, 0.589802952599454, 0.569464919751197, 0.54756242283769, 0.525659925924182, 0.503757429010675),
strata = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
type = "right")
km_data <- bind_rows(km_data, data.frame(strata = c(0,1),surv = c(1,1),
time = c(0,0)))
ggplot(km_data) +
geom_step(aes(x = time, y = surv, group = strata))
使用包 survminer
的 ggsurvplot
功能,它建立在 ggplot2
之上,可用于创建 Kaplan-Meier 图。
library(survminer)
km_data %>% ggsurvplot()
更新 1
新数据更新
km_data <- data.table(time = c(72, 109, 143),
n.risk = c(80, 77, 76),
n.event = c(1, 1, 2),
n.censor = c(3, 2, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974),
strata = c(0, 0, 0),
type = "right")
library(survminer)
km_data %>% ggsurvplot()