2

I'm trying to produce a cumulative incidence plot for a competing hazards survival analysis using plot() in R. For some reason, the plot that is produced has a legend that I have not called. The legend is intersecting with the lines on my graph and I can't figure out how to get rid of it. Please help!

My code is as follows:

CompRisk2 <- cuminc(ftime=ADI$time_DeathTxCensor, fstatus=ADI$status, group=ADI$natADI_quart)
cols <- c("darkorange","coral1","firebrick1","firebrick4","lightskyblue","darkturquoise","dodgerblue","dodgerblue4")
par(bg="white")
plot(CompRisk2,
     col=cols,
     xlab="Years",
     ylab="Probability of Mortality or Transplant",
     xlim=c(0,10),
     ylim=c(0,0.6))

Which produces the following plot: enter image description here

I tried adding the following code to move the legend out of the frame, but I got an error:

legend(0,5, legend=c(11,21,31,41,12,22,32,42),
col=c("darkorange","coral1","firebrick1","firebrick4","lightskyblue","darkturquoise","dodgerblue","dodgerblue4"),
lty=1:2, cex=0.8, text.font=4, box.lty=0)

Error: Error in title(...) : invalid graphics parameter

Any help would be much appreciated!

gcgoobie
  • 53
  • 2

2 Answers2

2

You are using the cuminc function from the cmprsk package. This produces an object of class cuminc, which has an S3 plot method. ?plot.cuminc shows you the documentation and typing plot.cuminc shows you the code.

There is some slightly obscure code that suggests a workaround:

 u <- list(...)
    if (length(u) > 0) {
        i <- pmatch(names(u), names(formals(legend)), 0)
        do.call("legend", c(list(x = wh[1], y = wh[2], legend = curvlab, 
            col = color, lty = lty, lwd = lwd, bty = "n", bg = -999999), 
            u[i > 0]))
    }

This says that any additional arguments passed in ... whose names match the names of arguments to legend will be passed to legend(). legend() has a plot argument:

plot: logical. If ‘FALSE’, nothing is plotted but the sizes are returned.

So it looks like adding plot=FALSE to your plot() command will work.

In principle you could try looking at the other arguments to legend() and see if any of them will adjust the legend position/size as you want. Unfortunately the x argument to legend (which would determine the horizontal position) is masked by the first argument to plot.cuminc.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • This is an extremely rare instance where you seem to have missed the obvious. I mention only because you are typically the "smartest kid on the block" w.r.t. regression packages, and I did find the answer useful if a bit incomplete. I think you were staring at the answer. – IRTFM Jun 11 '21 at 02:36
1

I don't think that the ellipsis arguments are intended for the legend call inside plot.cuminc. The code offered in Ben's answer suggests that there might be a wh argument that determines the location of the legend. It is not named within the parameters as "x" in the code he offered, but is rather given as a positionally-defined argument. If you look at the plot.cuminc function you do in fact find that wh is documented.

I cannot test this because you have not offered us access to the ADI-object but my suggestion would be to try:

opar <- par(xpd=TRUE) # xpd lets graphics be placed 'outside'
plot(CompRisk2,
     col=cols, wh=c(-.5, 7), 
     xlab="Years",
     ylab="Probability of Mortality or Transplant",
     xlim=c(0,10),
     ylim=c(0,0.6))
 par(opar)  # restores original graphics parameters

It's always a bit risky to put out a code chunk without testing, but I'm happy to report that I did find a suitable test and it seems to work reasonably as predicted. Using the code below on the object in the SO question prior question about using the gg-packages for cmprsk:

library(cmprsk)
# some simulated data to get started
comp.risk.data <- data.frame("tfs.days" = rweibull(n = 100, shape = 1, scale = 1)*100,
                             "status.tfs" = c(sample(c(0,1,1,1,1,2), size=50, replace=T)),
                             "Typing" = sample(c("A","B","C","D"), size=50, replace=T))

# fitting a competing risks model
CR <- cuminc(ftime = comp.risk.data$tfs.days, 
             fstatus = comp.risk.data$status.tfs, 
             cencode = 0,
             group = comp.risk.data$Typing)

opar <- par(xpd=TRUE) # xpd lets graphics be placed 'outside'
plot(CR,
      wh=c(-15, 1.1),  # obviously different than the OP's coordinates
     xlab="Years",
     ylab="Probability of Mortality or Transplant",
     xlim=c(0,400),
     ylim=c(0,1))
par(opar)   # restores graphics parameters

I get the legend to move up and leftward from its original position.

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487