在 ggsurvplot 上添加散点

Add scatter points on top of ggsurvplot

调用 ggsurvplot(...) 后,我想叠加另一个数据框 df 中的一些点,其中包含两列 timesurvival。我正在寻找完成此操作的技巧。

编辑:一些代码作为示例

require("survival")
require("survminer")
fit<- survfit(Surv(time, status) ~ sex, data = lung)

# Basic survival curves
ggsurvplot(fit, data = lung)

# Example points
x <- fit$time
y <- fit$n.risk

如何在 ggsurvplot 图上叠加 points(x, y)

ggplot 类型的对象是 ggsurvplot() 返回的对象的一部分,可以作为 $plot:

寻址
ggplot1 <- ggsurvplot(fit, data = lung)$plot

您可以像处理通常的 ggplot 对象一样使用它并添加其他图层。但是,对于您的具体示例,尚不清楚您要如何定义点的 Y 坐标:fit$n.risk 是 1 到 138 之间的数字,而您的绘图在 0..1 范围内。这是一个选项:

ggplot1 <- ggsurvplot(fit, data = lung)$plot
df1 <- data.frame(time=fit$time, nRisk=fit$n.risk, nRiskRel=fit$n.risk/max(fit$n.risk))  
ggplot1 + geom_point(aes(x=time, y=nRiskRel), data = df1, alpha=0.5, size=3)

您可能想要添加颜色等。