I am trying to follow this tutorial over here: https://rviews.rstudio.com/2017/09/25/survival-analysis-with-r/ (bottom of the page).
I have slightly modified the code for this tutorial and have plotted the "staircases" (i.e. "survival functions", in the below picture "red", "blue", "green") corresponding to 3 of the observations in the data:
library(survival)
library(dplyr)
library(ranger)
library(data.table)
library(ggplot2)
library(plotly)
a = na.omit(lung)
a$ID <- seq_along(a[,1])
r_fit <- ranger(Surv(time,status) ~ age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss, data = a, mtry = 4,
importance = "permutation", splitrule = "extratrees", verbose = TRUE)
death_times <- r_fit$unique.death.times
surv_prob <-data.frame(r_fit$survival)
avg_prob <- sapply(surv_prob, mean)
plot(r_fit$unique.death.times, r_fit$survival[1,], type = "l", ylim = c(0,1), col = "red", xlab = "Days", ylab = "survival", main = "Survival Curves")
new = a[1:3,]
pred <- predict(r_fit, new, type = 'response')$survival
pred <- data.table(pred)
colnames(pred) <- as.character(r_fit$unique.death.times)
plot(r_fit$unique.death.times, pred[1,], type = "l", col = "red")
lines(r_fit$unique.death.times, r_fit$survival[2,], type = "l", col = "green")
lines(r_fit$unique.death.times, r_fit$survival[3,], type = "l", col = "blue")
From here, I want to make the above plot as "interactive". I want to make so that when you move the mouse over one of the curves:
The "properties" belonging to that curve (from object "a") hover (e.g. ID, age, sex, ph.ecog, etc.)
In the same "hover box" from 1), also show the x-coordinate (r_fit$unique) and the y-coordinate (from "pred") for each position the mouse is hovering over (for a given curve)
My plan was first to take the "grob" object and convert it to a "ggplot" object, and then to convert the "ggplot" object into a "plotly" object:
grob= plot(r_fit$unique.death.times, pred[1,], type = "l", col = "red")
basic_plot = ggpubr::as_ggplot(grob)
But when I try to inspect the "basic_plot", it shows up as "NULL".
ggplot(f)
Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class gg/ggplot
Had this worked, I would have eventually converted the ggplot object to plotly:
plotly_plot = ggplotly(final_plot)
How to make this interactive plot?
I am trying to achieve something similar to this: https://plotly.com/python/v3/ipython-notebooks/survival-analysis-r-vs-python/ (towards the bottom of the page, plot with the title "lifespan of different tumor DNA profiles")
(Please Note: I am working with a computer that has no USB port or Internet connection, only R with a few pre-installed libraries... I do not have "ggplotify" or "survminer")
base
plots do not work as objects likeggplot
. You might needas.grob
cran.r-project.org/web/packages/ggplotify/vignettes/…. Or have you tried making the plot inggplot
orplotly
to begin with? – QAsenaggplot
and convert withggplotly
(or directly inplotly
) . I can't look at the moment from my phone but I suspect the issue is explained here stackoverflow.com/a/29583945/10142537. Maybe thegrob=plot()
returnsNULL
? – QAsenaggplot
code I've used is a basic example I can improve if you like. It would better fit theggplot
grammer for the data to be in a single dataframe (long data) and use onegeom_line
call rather than 3! – QAsena