Replicating a plot from the ROCR Website I'm trying to reproduce this plot from the ROCR website:

I can get something similar as follows:
library(ROCR)
data(ROCR.simple)
pred <- prediction(ROCR.simple$predictions, ROCR.simple$labels)
plot( performance(pred, "acc"))


But I can figure out what ROCR metric to use to get postive vs negative densities, and I also can't figure out how to combine 2 ROCR curves into one plot.  I figure since this figure shows up in several different places in the ROCR documentation, the code to replicate it must exist somewhere, correct?
 A: You just need to separate your predictions based on the labels: e.g.
good = rnorm(100, mean = 10)
bad = rnorm(1000, mean = 7)

Then use something like this:
plotOverlappingDensity <- function(a, b, cola='red', colb='darkgreen', ...){
  da = density(a, ...)
  db = density(b, ...)
  xlim = c( min(c(da$x, db$x)), max(c(da$x, db$x)) ) 
  ylim = c( min(c(da$y, db$y)), max(c(da$y, db$y)) ) 
  plot(da, col=cola, xlim=xlim, ylim=ylim, ...)
  lines(db, col=colb)
}

plotOverlappingDensity(good, bad)


A: Well, I answered my own question.  Running demo(ROCR) shows the code required to make the plot, but it's a little difficult to scrape out and parse.  I made a function for single-run, binary classifiers:
density_plot <- function(pred, pos=NULL, 
                         legend=c('negative', 'positive'), colors=c("red", "green")){

  stopifnot(require('ROCR'))
  stopifnot(length(pred@predictions)==1) #Multiple runs not supported
  lev <- levels(pred@labels[[1]])
  stopifnot(length(lev)==2) #Only binary classification supported for now

  if (is.null(pos)){
    pos <- lev[2]
  }
  neg <- setdiff(lev, pos)

  neg_col = colors[1]
  pos_col = colors[2]

  neg_dens <- density(pred@predictions[[1]][pred@labels[[1]]==neg])
  pos_dens <- density(pred@predictions[[1]][pred@labels[[1]]==pos])
  top <- ceiling(max(neg_dens$y, pos_dens$y))

  plot(0,0,type="n", xlim= c(0,1), ylim=c(0,top),
       xlab="Cutoff", ylab="Density",
       main="How well do the predictions separate the classes?")
  lines(neg_dens, col=neg_col)
  lines(pos_dens, col=pos_col)
  legend(0, top, legend=legend, col=c(neg_col,pos_col), lty=1)
}

I'm curious to see if there's a more elegant solution, that perhaps supports multiple-runs (like the rest of the functions in ROCR)
