Survival plots with plotly

1.5k views Asked by At

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

1

There are 1 answers

1
NicolasH2 On BEST ANSWER

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

plotly::ggplotly(p1[[1]])