Search code examples
rplotggplot2rocproc-r-package

pROC plot as object?


I would like to construct a rPOC plot OBJECT that I can pass to a function and plot later. In the example below (taken from here), I can certainly plot my roc curve. But I would like to create a plot OBJECT (say by defining g <- and later plotting it with plot(g)). It seems that the plot of ciobj, ci functions below will add to the original plot, but I can't make an OBJECT assembling these layers together. I have tried the 'add' argument, and creating new plot objects with the return values of these plot functions.

library(pROC)
data(aSAH)
rocobj <- plot.roc(aSAH$outcome, aSAH$s100b,  main="Confidence intervals", percent=TRUE,  ci=TRUE,  print.auc=TRUE)
ciobj <- ci.se(rocobj, specificities=seq(0, 100, 5))
plot(ciobj, type="shape", col="#1c61b6AA") 
plot(ci(rocobj, of="thresholds", thresholds="best"))

Solution

  • As MrFlick mentioned you can pass functions instead of object. Alternatively you can pass unevaluated function calls, and evaluate them within your function. For instance:

    library(pROC)
    data(aSAH)
    
    
    plot1 <- quote(plot(rocobj, main="Confidence intervals", print.auc=TRUE))
    plot2 <- quote(plot(ci.se(rocobj, specificities=seq(0, 100, 5)), type="shape", col="#1c61b6AA"))
    plot3 <- quote(plot(ci(rocobj, of="thresholds", thresholds="best")))
    
    doplots <- function(rocobj, calls) {
      for (call in calls) {
        eval(call)
      }
      invisible(rocobj)
    }
    
    roc1 <- roc(aSAH$outcome, aSAH$s100b, percent = TRUE)
    doplots(roc1, list(plot1, plot2, plot3))
    
    roc2 <- roc(aSAH$outcome, aSAH$wfns, percent = TRUE)
    doplots(roc2, list(plot1, plot3))
    

    There is no limit to what you can do, except your patience with R's evaluation rules.