Reputation: 729
I am trying to create a nice-looking survival plot in R. Is it possible to add a vertical line to the survival plot created with ggsurvplot() function? I would like to show time thresholds in this way.
I've already tried using abline() function like in a code below.
surv_object <- Surv(time = data1$survive_time)
fit1 <- survfit(surv_object~sex,data = data1)
ggsurvplot(fit1, data = data1, pval = TRUE, xlab="Time [days]", risk.table="percentage",surv.median.line="hv")
abline(v=200)
However it gives me the following error:
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...): plot.new has not been called yet
Traceback:
1. abline(v = 120)
2. int_abline(a = a, b = b, h = h, v = v, untf = untf, ...)
Is there any way I can add such a line to the survival plot? Why it is not recognised as a plot?
Upvotes: 2
Views: 5832
Reputation: 46
ggsurvplot is an extension of ggplot2 so you'll want to use the ggplot functions for best results if you are adding anything to the plot. The only annoying hangup is that ggsurvplots are stored as an object that has multiple items in it (including the ggplot, called plot). Once you know this adding the vertical line to ggsurvplot is easy:
yoursurvplot<-ggsurvplot(fit1, data = data1, pval = TRUE, xlab="Time [days]", risk.table="percentage",surv.median.line="hv")
yoursurvplot$plot + geom_vline(xintercept = 200)
This worked for me when I was adding a vertical line for the 5 year survival time for two cancer cohorts I was comparing.
Upvotes: 3