2

there! I try use package "plotly" to make interactive my survival curve, so for reproducible example, I use "lung" dataset:

library(survival)
library(survminer)
library(plotly)
sf_lung <- survival::survfit(survival::Surv(time, status) ~ 1, data = lung)
p1 <- ggsurvplot(sf_lung, main = "Kaplan-Meier Curve for the NCCTG Lung Cancer Data")
plotly::ggplotly(p1)

And that error I get:

Error in UseMethod("ggplotly", p) : no applicable method for 'ggplotly' applied to an object of class "c('ggsurvplot', 'ggsurv', 'list')"

So what wrong?
my session info:
R version 4.0.2 (2020-06-22){...}
other attached packages:
survival_3.2-7 survminer_0.4.8 ggpubr_0.4.0 plotly_4.9.2.1 ggplot2_3.3.2

TeoK
  • 511
  • 6
  • 13
  • `ggplotly` doesn't know how to handle plots of that type since they aren't "normal" ggplots. Perhaps this package can help: https://github.com/jakeybob/plotly-survival or see the code here: https://plotly.com/python/v3/ipython-notebooks/survival-analysis-r-vs-python/ – MrFlick Oct 19 '20 at 08:41

1 Answers1

7

ggsurvplot created a list object. Use only the first item ("plot") At least that works for me.

plotly::ggplotly(p1[[1]])
NicolasH2
  • 774
  • 5
  • 20
  • Thanks! work for me, but I still have one issue: risk table, now its gone, and plotly don't recognize it – TeoK Oct 20 '20 at 08:03