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 "+"
Use
%++%instead of+. See?add_ggsurvplotfor more details.To remove labels at the end of the table, we can assign
surv_km[["table"]][["layers"]][[2]]toNULL, e.g.Finally to remove
disease=in graph's and/or table's legend labels, usegsubwithnames(fit$strata), as so