无法使用ggsurvplot从列表中绘制带有survfit对象的kaplan-meier曲线 [英] unable to plot kaplan-meier curve with survfit object from a list using ggsurvplot

查看:734
本文介绍了无法使用ggsurvplot从列表中绘制带有survfit对象的kaplan-meier曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用survminer软件包中的ggsurvplot绘制Kaplan-Meyer曲线.当我传递保存在列表中的survfit对象时,无法绘制它.

I am trying to plot Kaplan-Meyer curve using ggsurvplot from survminer package. I'm unable to plot it when I pass a survfit object saved in a list.

让我以肺部数据集为例.一切都在下面进行:

Let me use lung dataset as a example. Everything works below:

library("survival")
library("survminer")
fit <- survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit,
          conf.int = TRUE,
          risk.table.col = "strata", 
          palette = c("#E7B800", "#2E9FDF"),
          xlim = c(0, 600))

现在,我确实对两个变量进行了拟合,并将模型结果保存在列表中.然后尝试使用ggsurvplot制作KM图.

Now I do survfit on two variables and save the model result in a list. Then tried to make KM plot with ggsurvplot.

vars <- c('sex', 'ph.ecog')
l<- map (vars, ~survfit(Surv(time, status)~ get(.x),data = lung ))
l<- set_names(l, vars)
ggsurvplot(l$sex,
          conf.int = TRUE,
          risk.table.col = "strata", 
          palette = c("#E7B800", "#2E9FDF"),
          xlim = c(0, 600))

我收到了这样的错误消息:

I got error message like this:

Error in eval(inp, data, env) : object '.x' not found

有人知道为什么吗?我该如何解决这个问题?非常感谢!

Does someone know why? How can I fix this problem? Thanks a lot!

推荐答案

第一个需要加载一个或多个所需的软件包.我想这几天很多用户认为运行R意味着每个人都应该拥有tidyverse,但这不是真的.

First one would need to load the package or packages needed. I suppose these days many users think that running R means that everyone is presumed to have the tidyverse in place, but that is NOT true.

 library(tidyverse)
 # run both your code segments, since you will need a small piece of first one
str(l$sex)
List of 14
 $ n        : int [1:2] 138 90
 $ time     : num [1:206] 11 12 13 15 26 30 31 53 54 59 ...
 $ n.risk   : num [1:206] 138 135 134 132 131 130 129 128 126 125 ...
 $ n.event  : num [1:206] 3 1 2 1 1 1 1 2 1 1 ...
 $ n.censor : num [1:206] 0 0 0 0 0 0 0 0 0 0 ...
 $ surv     : num [1:206] 0.978 0.971 0.957 0.949 0.942 ...
 $ type     : chr "right"
 $ strata   : Named int [1:2] 119 87
  ..- attr(*, "names")= chr [1:2] "get(.x)=1" "get(.x)=2"
 $ std.err  : num [1:206] 0.0127 0.0147 0.0181 0.0197 0.0211 ...
 $ upper    : num [1:206] 1 0.999 0.991 0.987 0.982 ...
 $ lower    : num [1:206] 0.954 0.943 0.923 0.913 0.904 ...
 $ conf.type: chr "log"
 $ conf.int : num 0.95
 $ call     : language survfit(formula = Surv(time, status) ~ get(.x), data = lung)
 - attr(*, "class")= chr "survfit"

因此,当您看到strata名称"属性时,其中就有一个get(调用,这似乎使ggsurvplot的逻辑受阻.使用attr<-将其替换为内容更丰富的内容(而"y语言"则较少).

So when you see that strata "names"-attribute, it's got a get(-call in it and that appears to choke the logic of ggsurvplot. Use attr<- to replace it with something more informative (and less-"language-y").

attr(l[['sex']][['strata']], "names") <- c("sex=1", "sex=2")

该表达式也位于"call"-叶中,因此您将需要用更易处理的内容替换它.我认为用"call" leaf from the first fit`-object yoiu替换它很容易做到:

That expression is in the "call"-leaf as well, so you will need to replace it with something more tractable. I think that's easy to do by replacing it with the "call" leaf from the firstfit`-object yoiu made:

l$sex$call <- fit$call
ggsurvplot(l$sex,
          conf.int = TRUE,
          risk.table.col = "strata", 
          palette = c("#E7B800", "#2E9FDF"),
          xlim = c(0, 600))

这篇关于无法使用ggsurvplot从列表中绘制带有survfit对象的kaplan-meier曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆