r - 将两条 Kaplan Meier 曲线与 ggsurvplot 合并
问题描述
我有两个不同的数据集,它们基本上包含相同的数据,但一个是针对基线年龄 19 岁或以下 ( data.all.agefs.under19
),另一个是针对 19 岁以上 ( data.all.agefs.above19
)
每个人的生存对象定义为:
surv.all.agefs.under19 <- Surv(time = data.all.agefs.under19$follow.up.years, event = data.all.agefs.under19$death.specific)
surv.all.agefs.above19 <- Surv(time = data.all.agefs.above19$follow.up.years, event = data.all.agefs.above19$death.specific)
Cox PH 模型定义为:
cox.all.agefs.under19 <- coxph(surv.all.agefs.under19 ~ factor1 + factor2 + factor3, data = data.all.agefs.under19)
cox.all.agefs.above19 <- coxph(surv.all.agefs.above19 ~ factor1 + factor2 + factor3, data = data.all.agefs.above19)
我想为两者创建一个带有 Kaplan Meier 曲线的图,但到目前为止,我只能使用 ggsurvplot 为每个曲线创建一个图:
ggsurv <- ggsurvplot(survfit(cox.all.agefs.under19), data = data.all.agefs.under19, palette = "#2E9FDF", ggtheme = theme_minimal(), legend = "none")
ggsurv <- ggsurvplot(survfit(cox.all.agefs.above19), data = data.all.agefs.above19, palette = "#2E9FDF", ggtheme = theme_minimal(), legend = "none")
那么如何将两条曲线合并到同一个图中呢?
解决方案
您可以将数据附加到一个长数据框中,并定义一个变量agegrp
来区分两个年龄组。然后你可以绘制如下图。
df1 <- lung
df1$agegrp <- df1$sex
fitme <- survfit(Surv(time, status) ~ agegrp, data = df1)
ggsurv2 <- plot(fitme ,
xlim = c(0,1200),
main = "Survival curves based on Kaplan-Meier estimates",
xlab = "Time in days", # customize X axis label.
ylab = "Overall survival probability" # Y axis label
)
temp <- lines(fitme, lwd=2:1, col = c("red", "blue"))
text(temp, c("Under19", "Above19"), adj= -.1) # labels just past the ends
更新:
使用 ggsurvplot 如下所示
ggsurv <- ggsurvplot(
fitme, # survfit object with calculated statistics.
data = df1, # data used to fit survival curves.
risk.table = TRUE, # show risk table.
pval = TRUE, # show p-value of log-rank test.
conf.int = TRUE, # show confidence intervals for
# point estimates of survival curves.
palette = c("#E7B800", "#2E9FDF"),
xlim = c(0,1200), # present narrower X axis, but not affect
# survival estimates.
xlab = "Time in days", # customize X axis label.
break.time.by = 100, # break X axis in time intervals by 500.
ggtheme = theme_light(), # customize plot and risk table with a theme.
risk.table.y.text.col = T,# colour risk table text annotations.
risk.table.height = 0.25, # the height of the risk table
risk.table.y.text = FALSE,# show bars instead of names in text annotations
# in legend of risk table.
ncensor.plot = TRUE, # plot the number of censored subjects at time t
ncensor.plot.height = 0.25,
conf.int.style = "step", # customize style of confidence intervals
surv.median.line = "hv", # add the median survival pointer.
legend.labs = c("Under19", "Above19") # change legend labels.
)
推荐阅读
- reactjs - 反应 - 全选删除时输入更改未触发
- java - Android开发,如何在按下控制器按钮时更改自定义类的成员的值
- java - 首先从输入中获取整数列表的程序
- python - 如何通过单击 Python 中的 Tkinter 按钮来接受图像作为用户输入?
- ios - 任何人都可以帮我解决这个问题吗?
- breakpoints - 在 768 处覆盖 Bootstrap 3 Navbar 断点
- reactjs - 删除图表 js 的 x 轴上的重复值
- python - 将值添加/附加到列表中的特定索引
- multithreading - 有没有一种标准的方法来编写这个紧密耦合的 for 循环,这样它就不会烧掉我 100% 的 CPU
- reactjs - 使用 reactjs 禁用带有复选框的输入表单