You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为单组Kaplan-Meier生存曲线0-3650天区间下区域着色?

为Kaplan-Meier生存曲线指定区间填充颜色的解决方案

Base R 实现

通过提取生存曲线的坐标点,补充区间端点后使用polygon()函数完成填充:

library(survival)
data(myeloma)

# 拟合生存曲线
fit2 <- survfit(Surv(entry, futime, death) ~ 1, myeloma)

# 提取曲线的时间点与生存概率
time_points <- fit2$time
surv_probs <- fit2$surv

# 构建填充区域的坐标:包含0天、所有<=3650天的时间点、3650天
x_fill <- c(0, time_points[time_points <= 3650], 3650)
# 获取3650天对应的生存概率
surv_3650 <- predict(fit2, times = 3650)$surv
y_fill <- c(1, surv_probs[time_points <= 3650], surv_3650)

# 绘制曲线并填充区域
plot(fit2, conf.int = FALSE, main = "Kaplan-Meier生存曲线(0-3650天区域填充)")
polygon(x_fill, y_fill, col = rgb(0, 0, 1, 0.2), border = NA) # 半透明蓝色填充
abline(v = 3650, col = "blue")

ggplot2 实现

借助broom包将生存曲线对象转换为数据框,再用geom_area()完成填充:

library(survival)
library(ggplot2)
library(broom)
data(myeloma)

# 拟合生存曲线
fit2 <- survfit(Surv(entry, futime, death) ~ 1, myeloma)

# 整理填充区域的数据:补充0天和3650天的坐标
fill_data <- tidy(fit2) %>%
  filter(time <= 3650) %>%
  add_row(time = 0, estimate = 1, .before = 1) %>%
  add_row(time = 3650, estimate = predict(fit2, times = 3650)$surv)

# 绘制曲线与填充区域
ggplot() +
  # 绘制完整的Kaplan-Meier阶梯曲线
  geom_step(data = tidy(fit2), aes(x = time, y = estimate), direction = "hv") +
  # 填充0-3650天的曲线下区域
  geom_area(data = fill_data, aes(x = time, y = estimate), fill = "blue", alpha = 0.2) +
  # 添加3650天的竖线
  geom_vline(xintercept = 3650, color = "blue") +
  labs(title = "Kaplan-Meier生存曲线(0-3650天区域填充)",
       x = "时间(天)",
       y = "生存概率") +
  theme_minimal()

内容的提问来源于stack exchange,提问作者CharlesLDN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 05:47:46