本文介绍了R:Tibble vs ggplot2(图表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循R中的教程(https://rviews.rstudio.com/2017/09/25/survival-analysis-with-r/).我用于工作的计算机没有USB端口或互联网连接-仅具有安装了一些库的R.我的工作计算机具有生存,游侠,ggplot2和dplyr".但是,它没有"ggfortify".我试图弄清楚如何在没有"ggfortify"的情况下从本教程中绘制图形.这是我在下面使用的代码:

I am trying to follow a tutorial in R (https://rviews.rstudio.com/2017/09/25/survival-analysis-with-r/).The computer I am using for work does not have a USB port or internet connection - it only has R with a few libraries installed. My work computer has "survival, ranger, ggplot2 and dplyr". However, it does not have "ggfortify". I am trying to figure out how to plot the graphs from the tutorial without 'ggfortify'. Here is the code I am using below:

  #load libraries
    library(survival)
    library(ranger)
    library(ggplot2)
    library(dplyr)

#load data
data(veteran)
head(veteran)

#Part 1 : works
# Kaplan Meier Survival Curve
km <- with(veteran, Surv(time, status))
km_fit <- survfit(Surv(time, status) ~ 1, data=veteran)

#plot(km_fit, xlab="Days", main = 'Kaplan Meyer Plot') #base graphics is always ready

tibble(time = km_fit$time, surv = km_fit$surv,
       min = km_fit$lower, max = km_fit$upper) %>%
  ggplot(aes(x = time)) +
  geom_line(aes(y = surv)) +
  geom_ribbon(aes(ymin = min, ymax = max), alpha = 0.3)

但是,我无法执行此操作:

However, I can't get this to work:

#Part 2: does not work


km_trt_fit <- survfit(Surv(time, status) ~ trt, data=veteran)



tibble(time = km_trt_fit$time, surv = km_trt_fit$surv,
       min = km_trt_fit$lower, max = km_trt_fit$upper) %>%
    ggplot(aes(x = time, group = factor(veteran$trt), colour = factor(veteran$trt), fill = factor(veteran$trt))) +
    geom_line(aes(y = surv)) +
    geom_ribbon(aes(ymin = min, ymax = max), alpha = 0.3)


Error: Aesthetics must be either length 1 or the same as the data (114): group, colour and fill

或者这可以工作:
#Part 3:不起作用

Or this to work:
#Part 3: does not work

vet <- mutate(veteran, AG = ifelse((age < 60), "LT60", "OV60"),
              AG = factor(AG),
              trt = factor(trt,labels=c("standard","test")),
              prior = factor(prior,labels=c("N0","Yes")))

aa_fit <-aareg(Surv(time, status) ~ trt + celltype +
                 karno + diagtime + age + prior ,
                 data = vet)

tibble(time = aa_fit$time, surv = aa_fit$surv,
       min = aa_fit$lower, max = aa_fit$upper) %>%
  ggplot(aes(x = time)) +
  geom_line(aes(y = surv)) +
  geom_ribbon(aes(ymin = min, ymax = max), alpha = 0.3)

Error: geom_line requires the following missing aesthetics: y

有人可以帮我纠正这些问题吗?

Can someone please help me correct these?

谢谢(上一篇文章: R:绘制图形(ggplot与自动绘图))

推荐答案

您将不得不做一些侦探工作!

You are going to have to do some detective work!

我今天有时间参加第二部分.因此:事实证明,有关层的信息包含在元素 km_trt_fit $ strata 中.看起来像这样:

I have time for part #2 today. So: It turns out, that the information about the strata is contained in the element km_trt_fit$strata. It looks like this:

km_trt_fit <- survfit(Surv(time, status) ~ trt, data=veteran)

km_trt_fit$strata

#> trt=1 trt=2
#>    61    53

这告诉您,有 trt = 1 的61个元素和 trt = 2 的53个元素.我不知道为什么这些加起来不等于137(退伍军人中的行数),但是我认为这正是 survfit()的工作方式.这也是导致错误的原因,因为生成的模型数据与原始数据框的行数不同,而您要通过使用 veteran $ trt 尝试包含这些数据.

This is telling you that there are 61 elements of trt=1 and 53 elements of trt=2. I don't know why these don't add up to 137 (the number of rows in veteran) but I assume that's just how survfit() works. It is also the reason you are getting the error, because the resulting model data have a different number of rows than the original data frame, which you are trying to include by using veteran$trt.

我的解决方案:创建一个向量 strata ,分别包含61个和53个元素,分别是 trt = 1 trt = 2 :

My solution: Create a vector strata with 61 and 53 elements of trt=1 and trt=2 respectively:

strata = km_trt_fit$strata
strata = rep(names(strata), times = strata)

在您的输入数据中包括它:

Include that in your input data:

tibble(time = km_trt_fit$time,
       surv = km_trt_fit$surv,
       min  = km_trt_fit$lower,
       max  = km_trt_fit$upper,
       trt  = factor(strata)) %>%
  ggplot(aes(x = time, colour = trt, fill = trt)) +
  geom_line(aes(y = surv)) +
  geom_ribbon(aes(ymin = min, ymax = max), alpha = 0.3)

结果与本教程的结果非常接近.

The result is pretty close to what the tutorial has.

我对ggfortify不太熟悉,但是它的工作可能是自动为您做类似的事情.在没有它的情况下,您将不得不研究由模型函数生成的结构,并像上面一样手动提取数据.

I am not overly familiar with ggfortify but its job is probably to do something similar for you automagically. In its absence, you will have to investigate the structures produced by the model functions and extract the data manually like I did above.

这篇关于R:Tibble vs ggplot2(图表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:59