简体   繁体   English

使用 survminer::ggsurvplot 在 r 中以编程方式绘制许多生存曲线的问题

[英]issues using survminer::ggsurvplot to plot many survival curves programmatically in r

I can plot a single Kaplan-Meier plot like below with ggsurvplot:我可以使用 ggsurvplot 绘制如下所示的单个 Kaplan-Meier 图:

library(survminer)
library(survival)
fit1 = survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit1, data = lung)

However, I need to plot many KM plot programmatically.但是,我需要以编程方式绘制许多 KM 图。 I need to pass different variables as strings.我需要将不同的变量作为字符串传递。 I tried below.我在下面试过。

fml = as.formula(paste('Surv(time, status)~', 'sex'))
fit2 = survfit(fml, data = lung)
ggsurvplot(fit2, data = lung)

surprisingly, this does not work.令人惊讶的是,这不起作用。 I got the error message below:我收到以下错误消息:

Error: object of type 'symbol' is not subsettable

I don't know why this happens.我不知道为什么会发生这种情况。 Does anyone know how to fix this?有谁知道如何解决这一问题? Thanks a lot.非常感谢。

As suggested at the link in Aidan's comment , you need to use the function survminer::surv_fit() , which is a wrapper around survival::survfit() .由于在链接建议艾丹的评论,你需要使用的功能survminer::surv_fit()这大约是一个包装survival::survfit() So, in your example,所以,在你的例子中,

library(survminer)
library(survival)
 # lung is distributed as an object, survival::lung
fit1 = surv_fit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit1, data = lung)

The output from surv_fit(object.formula) can then be plotted然后可以绘制来自surv_fit(object.formula)的输出

fml = as.formula(paste('Surv(time, status)~', 'sex'))
fit2 = surv_fit(fml, data = lung)
ggsurvplot(fit2, data = lung)

In the surv_fit help page it also shows how to fit a list of formulae.surv_fit帮助页面中,它还显示了如何拟合公式列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM