R: plotagem de gráficos (ggplot vs autoplot)

Nov 27 2020

Estou seguindo um tutorial R aqui https://rviews.rstudio.com/2017/09/25/survival-analysis-with-r/

O computador que utilizo para o trabalho não tem acesso à Internet nem porta USB - tem apenas R com algumas bibliotecas pré-instaladas. O tutorial requer "sobrevivência", "ggplot2", "ranger", "dplyr" e "ggfortify". O computador que uso para trabalhar tem todas essas bibliotecas, EXCETO ggfortfiy. Aparentemente, uma função chamada "autoplot" é necessária da biblioteca ggfortify para fazer alguns dos gráficos neste tutorial.

Quando tento executar o código do tutorial:

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

#load data
data(veteran)
head(veteran)

# 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


#here is where the error is 
autoplot(km_fit)

Eu obtenho o seguinte erro: Error: Objects of type survfit not supported by autoplot.

Alguém sabe como consertar isso? É possível fazer plotagens semelhantes sem a biblioteca ggfortify? Pode ser feito apenas com ggplot2?

No meu computador pessoal, posso fazer este gráfico depois de instalar a biblioteca ggfortify.

(nota: eu também não tenho a biblioteca "sobrevivente")

obrigado

Respostas

Till Nov 27 2020 at 16:28

Sim, isso é possível, porque a autoplotfunção usa ggplot2sob o capô:

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)