1

I am working with survival analysis and the smoothHR package, after modeling I'd like to plot the relative risk vs a variable, thing that is quite easy with

plot(dataset, predictor)

But I'd like to do it using the ggplot package. Any idea how to?

#the library
library(smoothHR)

#the artificial dataset
surv.days<- runif(n = 200, min = 100, max = 500)
censor<- sample(c(0,1), 200, replace=TRUE)
surv.var<- surv.days/10 + rnorm(200, mean = 0, sd = 3)
surv.var[which(surv.days>250)]<- surv.days[which(surv.days>250)]/5 + rnorm(length(which(surv.days>250)), mean = 0, sd = 10)

survdata<- data.frame(surv.days, censor, surv.var)

rm(censor, surv.days, surv.var)

#using smoothHR package to adjust a model
variabledf<-dfmacox (time = "surv.days", status = "censor",
                    nl.predictor = c ("surv.var"),
                    smoother = "ns",
                    method = "AIC",
                    data = survdata)

coxmodel<- coxph(Surv(surv.days, censor) ~ ns(surv.var, variabledf$df[1]), data = survdata, x = TRUE)

c.smoothhr<-smoothHR (data = survdata, coxfit = coxmodel)

After that, I can plot the risk as a function of the survival variable

plot (c.smoothhr, predictor = "surv.var", conf.level = 0.95, ref.label = "", main = "", xlab = "surv.var")

I would like to generate this plot using the ggplot2 package, for storing and customization purposes; but I am simply clueless about how to proceed.

Dario.gs
  • 65
  • 4
  • 3
    This looks like it will be a pain. You can look at `plot.HR` to see all the processing it does before plotting - quite a lot. Essentially you want to edit that function to return the processed data that it plots instead of making a plot, and then you can use that data with ggplot. – Gregor Thomas Dec 19 '17 at 19:29
  • Thanks, I've chosen to give up the editing part and simply force the plot into a Grob by following [this post](https://stackoverflow.com/questions/29583849/r-saving-a-plot-in-an-object) – Dario.gs Dec 20 '17 at 13:35

0 Answers0