4

The following code was taken from @adibender answer to "Multiple ROC curves in one plot ROCR". The code is partly from ?plot.performance.

library(ROCR)
data(ROCR.simple)
preds <- cbind(p1 = ROCR.simple$predictions, 
            p2 = abs(ROCR.simple$predictions + 
            rnorm(length(ROCR.simple$predictions), 0, 0.1)))

pred.mat <- prediction(preds, labels = matrix(ROCR.simple$labels, 
            nrow = length(ROCR.simple$labels), ncol = 2) )

perf.mat <- performance(pred.mat, "tpr", "fpr")
plot(perf.mat)

I want to illustrate several ROC curves in a single plot, like the code above, using the r package ROCR. However, i would want the ROC curves to be in different colours. How do i apply different colours to different curves? Thanks, in advance.

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
warnbergg
  • 552
  • 4
  • 14

3 Answers3

8

Try this (you can do it with ROCR):

library(ROCR)
data(ROCR.simple)
preds <- cbind(p1 = ROCR.simple$predictions, 
               p2 = abs(ROCR.simple$predictions + 
                          rnorm(length(ROCR.simple$predictions), 0, 0.1)))
n <- 2 # you have n models
colors <- c('red', 'blue') # 2 colors
for (i in 1:n) {
   plot(performance(prediction(preds[,i],ROCR.simple$labels),"tpr","fpr"), 
                                           add=(i!=1),col=colors[i],lwd=2)
}

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
3

The plot function for ROCR objects does not seem to offer this option. So, you'll have to pick apart the object and do it manually.

For instance, using ggplot2,

library(ggplot2)
df <- data.frame(Curve=as.factor(rep(c(1,2), each=length(perf.mat@x.values[[1]]))), 
                 FalsePositive=c(perf.mat@x.values[[1]],perf.mat@x.values[[2]]),
                 TruePositive=c(perf.mat@y.values[[1]],perf.mat@y.values[[2]]))
plt <- ggplot(df, aes(x=FalsePositive, y=TruePositive, color=Curve)) + geom_line()
print(plt)
mpjdem
  • 1,504
  • 9
  • 14
0

You can use just plot with add=TRUE

pred <- prediction( predicted1, response )
pred2 <- prediction(predicted2, response)
perf <- performance( pred, "tpr", "fpr" )
perf2 <- performance(pred2, "tpr", "fpr")
plot( perf, colorize = FALSE,  type="l",col="blue")
abline(a=0,b=1)
plot(perf2,colorize = FALSE, add = TRUE,  type="l", lty=3,col="black")
Blurryface
  • 11
  • 2