I can plot a single Kaplan-Meier plot like below with ggsurvplot:
library(survminer)
library(survival)
fit1 = survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit1, data = lung)
However, I need to plot many KM plot programmatically. I need to pass different variables as strings. I tried below.
fml = as.formula(paste('Surv(time, status)~', 'sex'))
fit2 = survfit(fml, data = lung)
ggsurvplot(fit2, data = lung)
surprisingly, this does not work. I got the error message below:
Error: object of type 'symbol' is not subsettable
I don't know why this happens. Does anyone know how to fix this? Thanks a lot.