Animated Survival Curves

1 Import your data set

library(readxl)
datos<-read_excel("Downloads/HC.xlsx")

2 Create an estimation using survfit function

library(survival)
fit<-survfit(Surv(datos$OS, datos$Death) ~ datos$Tumorsize, data = datos)

# datos$OS is the time variable
# datos$Death indicates 1 if the patient is dead, 0 if he is not (or unknown)
# datos$Tumorsize is a covariable that indicates the size of tumor (categorical: <=5 or >5)

3 Create data frame with your estimation

risk<-as.vector(fit$n.risk)
time<-as.vector(fit$time)
surv<-as.vector(fit$surv)
n1<- as.integer(fit$strata[1])  #number of patients with tumor <=5
n2<-as.integer(fit$strata[2])  #number of patients with tumor >5
grupo<-c(rep(1,n1),rep(2,n2))
mi_data<-as.data.frame(cbind(time,surv,round(surv,4),grupo,risk))

4 Create animated plot

Option 1: using plotly

2) Using gganimate and gapmider