“non-numeric argument to binary operator” when using ggsurvplot() + geom_dl()

695 Views Asked by At

I would like to attach labels using geom_dl to a Kaplan–Meier (KM) plot produced by ggsurvplot. It works fine if I use the plot part of ggsurvplot.

library(survival)
library(survminer)
library(directlabels)

fit <- survfit(Surv(time, status) ~ disease, data = kidney)
surv_km <- ggsurvplot(fit, risk.table = TRUE)
surv_km$plot + geom_dl(aes(label = gsub('disease=','',strata)), 
                       method = list(dl.trans(x = x + .2), "last.points"))

However, I get an error If I use the complete surv_km

surv_km + geom_dl(aes(label = gsub('disease=','',strata)), 
                  method = list(dl.trans(x = x + .2), "last.points"))
>Error in surv_km + geom_dl(aes(label = gsub("disease=", "", strata)),  : 
  non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.ggsurv", "+.gg") for "+"
1

There are 1 best solutions below

0
A. Suliman On BEST ANSWER

Use %++% instead of +. See ?add_ggsurvplot for more details.

fit <- survfit(Surv(time, status) ~ disease, data = kidney)
surv_km <- ggsurvplot(fit, risk.table = TRUE, legend = "none")
surv_km %++% geom_dl(aes(label = gsub('disease=','',strata)), 
                     method = list(dl.trans(x = x + .2), "last.points"))

enter image description here

To remove labels at the end of the table, we can assign surv_km[["table"]][["layers"]][[2]] to NULL, e.g.

surv_km <- surv_km %++% 
               geom_dl(aes(label = gsub('disease=','',strata)), 
                       method = list(dl.trans(x = x + .2), "last.points"))
surv_km[["table"]][["layers"]][[2]]<-NULL

Finally to remove disease= in graph's and/or table's legend labels, use gsub with names(fit$strata), as so

attr(fit$strata, "names") = gsub("disease=","",attr(fit$strata, "names"))
#then repeat above steps