ggsurvplot:从函数调用时无法使用 survfit

cam*_*sia 5 r ggplot2

我正在尝试绘制生存图,并在尝试将我的 survfit 函数移动到主函数中时遇到了问题,我可以在主函数中为不同的数据集调用它。当我运行代码时

fit<- survfit(Surv(time, status) ~ sex, data = lung)

allsurv <- function(fit){
ggsurvplot(
fit,
pval = TRUE,
pval.coord = c(200, 0.10), 
conf.int = TRUE,
xlab = "Days",
ggtheme = theme_light(), 
surv.median.line = "hv", 
legend.labs = c("Female","Male"),
legend.title = "",
palette = c("#8C3F4D","#3E606F")) + 
scale_y_continuous(expand = c(0.02, 0.02),breaks = seq(from = 0, to = 1, by = 0.1),labels=percent) + 
scale_x_continuous(expand = c(0.006, 0.006),
                   limits = c(0,366*12), breaks = seq(0, 4392, 100))
}

allsurv(fit)
Run Code Online (Sandbox Code Playgroud)

函数正常绘制在此输入图像描述

但是,当我从函数调用 survfit 时:

fit_all <- function(x){
 survfit(Surv(time, status) ~ sex, data = x)
}

allsurv(fit_all(lung))
Run Code Online (Sandbox Code Playgroud)

我收到错误:“eval(fit$call$data) 中的错误:未找到对象 'x'”

对我做错了什么有什么想法吗?

axi*_*tic 3

Survminer 包含一个surv_fit充当survfit. 如果您使用surv_fit而不是survfit,则返回对象的“调用”将包括整个数据框而不仅仅是data = x。在函数内调用 ggsurvplot 时效果更好:

https://www.rdocumentation.org/packages/survminer/versions/0.4.6/topics/surv_fit

allsurv <- function(fit){
ggsurvplot(
fit,
pval = TRUE,
pval.coord = c(200, 0.10), 
conf.int = TRUE,
xlab = "Days",
ggtheme = theme_light(), 
surv.median.line = "hv", 
legend.labs = c("Female","Male"),
legend.title = "",
palette = c("#8C3F4D","#3E606F"))
}

fit_all <- function(x){
 surv_fit(Surv(time, status) ~ sex, data = x)
}

allsurv(fit_all(lung))
Run Code Online (Sandbox Code Playgroud)