-
Notifications
You must be signed in to change notification settings - Fork 7
Description
拟推荐图表名:kaplan-meier plot
图表分类:basic
图效果示例参考代码:# 加载包
library(survival)
library(survminer) # 提供ggsurvplot()函数
library(ggplot2)生成模拟生存数据
set.seed(123)
time <- rexp(100, rate = 0.1) # 生存时间
status <- sample(0:1, 100, replace = TRUE) # 事件状态(0=删失,1=事件)
group <- sample(c("Treatment", "Control"), 100, replace = TRUE) # 分组变量创建生存对象
surv_obj <- Surv(time, status)
获取生存曲线数据
fit <- survfit(surv_obj ~ group, data = data.frame(time, status, group))
surv_data <- surv_summary(fit)使用ggplot2直接绘制
ggplot(surv_data, aes(x = time, y = surv, color = strata)) +
geom_step(linewidth = 1.2) + # 阶梯线绘制生存曲线
geom_ribbon(
aes(ymin = lower, ymax = upper, fill =strata ),
alpha = 0.2,
color = NA # 隐藏置信区间边框
) +
scale_color_manual(
values = c("Control" = "#E7B800", "Treatment" = "#2E9FDF"),
name = "Group"
) +
labs(
title = "Kaplan-Meier Survival Curve",
x = "Time (days)",
y = "Survival Probability"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
legend.position = c(0.8, 0.8),
panel.grid.minor = element_blank()
) +添加删失标记
geom_point(
data = subset(surv_data, n.censor > 0),
aes(x = time, y = surv),
shape = "|", size = 4,
show.legend = FALSE
)