2
votes

Does anyone know a way to fit the cominc function from the cmprisk package to the ggplot2 package?

I am getting this error message:

Don't know how to automatically pick scale for object of type cuminc. Defaulting to continuous - Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : kan ikke tvinge klasse »"cuminc"« ind i en data.frame

  • ggplot2 doesn't know how to deal with data of class cuminc

I found a function to split the cuminc function so you can plot individual causes instead of both in the same plot:

cs.cuminc <- function(x,cause="1"){
  if (!is.null(x$Tests)) 
    x <- x[names(x) != "Tests"]
  which.out <- which(unlist(strsplit(names(x), " "))[seq(2,length(names(x))*2,2)]!=cause)
  x[which.out] <- NULL
  class(x) <- "cuminc"
  return(x)
}

This provides a plot with two curves - stil ggplot2 can't deal with the function.

1

1 Answers

2
votes

Move data from cuminc structure to a dataframe, then use ggplot geom_step():

ciplot <- function(c, title = "Cumulative incidence", xlim = -1, ylim = 1, ...)
{
  nc <- length(c)
  d <- data.frame()
  strata <- names(c)
  for (i in 1:nc) {
        d <- rbind(d, data.frame(time = c[[i]]$time, est = c[[i]]$est, var = c[[i]]$var, strat = strata[i]))
  }
  if (xlim == -1) xlim = max(d$time)
  ggplot(d, aes(time, est, col = strat)) + geom_step(size = 1) + ylim(0, 1) + xlim(0, xlim) + 
    ggtitle(title)
}