I have previously tried to ask for help but did not actually solve my problem. More info can be found here: (you can find the data-set here) https://stackoverflow.com/questions
This happens because the surv.plot
layer contains mappings for a variable called label
which is not included in the data for km.plot
. You should be able to get around this by adding your surv.plot
data as an argument to the geom
rather than ggplot()
when you create surv.plot
. This way the data needed to draw the layer will "travel with" it.
We can illustrate this with simpler data. Let's first create a plot from data with only a few columns:
library(tidyverse)
df1 <- mtcars %>%
select(mpg, wt)
# This represents `km.plot`
(p <- ggplot(df1, aes(wt, mpg)) + geom_point())
Now we can try to add a layer that relies on columns not included in df1
to
the previous plot:
df2 <- mtcars %>%
select(mpg, wt, cyl)
q1 <- ggplot(df2, aes(wt, mpg)) +
geom_smooth(aes(color = factor(cyl)), method = "lm")
p + q1$layers[[1]]
#> Error in factor(cyl): object 'cyl' not found
q2 <- ggplot() +
geom_smooth(data = df2, aes(wt, mpg, color = factor(cyl)), method = "lm")
p + q2$layers[[1]]
Created on 2018-07-23 by the reprex package (v0.2.0.9000).