# How can I draw median lines on a cumulative incidence plot in R and also include risk table / cumulative events table?

Here’s a repeatable example of the code I would like to execute:

``````library(survival)
library(survminer)

km.fit <- survfit(Surv(rfstime, status) ~ 1, data = gbsg)

ggsurvplot(km.fit, fun = function(x) 1-x, data = gbsg, censor = FALSE, conf.int = TRUE,
risk.table = TRUE, cumevents = TRUE, ylab = "Cumulative Incidence",
surv.median.line = "hv")
``````

This code doesn’t work and gives me the following error message:

``````Error in match(x, table, nomatch = 0L) :
'match' requires vector arguments
``````

What I want is what the following code produces, but with median lines added to it:

``````ggsurvplot(km.fit, fun = function(x) 1-x, data = gbsg, censor = FALSE, conf.int = TRUE,
risk.table = TRUE, cumevents = TRUE, ylab = "Cumulative Incidence")
``````

I figured out that I am getting the error mentioned previously because `surv.median.line` doesn’t work when a function is called. However, I do need to be plotting this as cumulative incidence, not as survival, so the function is necessary.

A recommended work-around was to try and draw the median lines manually in ggplot, which I can do once I have saved this ggplot object to a variable and called it in the following way:

``````myplot <- ggsurvplot(km.fit, fun = function(x) 1-x, data = gbsg, censor = FALSE, conf.int = TRUE,
risk.table = TRUE, cumevents = TRUE, ylab = "Cumulative Incidence")

myplot\$plot +
geom_segment(aes(x = 0, xend = 1810, y = 0.5, yend = 0.5), linetype = "dashed") +
geom_segment(aes(x = 1810, xend = 1810, y = 0, yend = 0.5), linetype = "dashed")
``````

However, NOW the problem is that I don’t see the risk table and cumulative incidence table anymore; I just have the plot. I do need the tables and it presents a lot of difficulty if I have to print them out and manage them separately somehow.

Any advice? How do I get everything I want (cumulative incidence plot, median lines, and tables) all in one plot? Is this even possible to do, or am I out of luck?

### >Solution :

The issue is that you manipulated the plot but did not assign it back to `ggsurvplot` object, i.e. do `myplot\$plot <- myplot\$plot + ...`. Then print `myplot` to get a plot of the survival curves inducing the risk table:

``````library(survival)
library(survminer)

km.fit <- survfit(Surv(rfstime, status) ~ 1, data = gbsg)

myplot <- ggsurvplot(km.fit,
fun = function(x) 1 - x, data = gbsg, censor = FALSE, conf.int = TRUE,
risk.table = TRUE, cumevents = TRUE, ylab = "Cumulative Incidence"
)

myplot\$plot <- myplot\$plot +
geom_segment(aes(x = 0, xend = 1810, y = 0.5, yend = 0.5), linetype = "dashed") +
geom_segment(aes(x = 1810, xend = 1810, y = 0, yend = 0.5), linetype = "dashed")

myplot
``````