Search code examples
rplotroc

Plot 3 ROC curves in a single graph in R


I would like to plot 3 ROC curves in a single graph in R. I use the pROC package. The resulting plot only shows the two curves. The code I use is:

library(pROC) # install with install.packages("pROC")
library(randomForest) # install with install.packages("randomForest")
set.seed(420) # this will make my results match yours
num.samples <- 100
weight <- sort(rnorm(n=num.samples, mean=172, sd=29))
weight1 <- sort(rnorm(n=num.samples, mean=112, sd=11))
obese <- ifelse(test=(runif(n=num.samples) < (rank(weight)/num.samples)), 
                yes=1, no=0)
glm.fit=glm(obese ~ weight, family=binomial)
lines(weight, glm.fit$fitted.values)
glm.fit1=glm(obese ~ weight1, family=binomial)
lines(weight1, glm.fit1$fitted.values)
rf.model <- randomForest(factor(obese) ~ weight)
    roc(obese, glm.fit$fitted.values, plot=TRUE, legacy.axes=TRUE, percent=TRUE, xlab="False Positive Percentage", ylab="True Postive Percentage", col="#377eb8", lwd=4, print.auc=TRUE) 
    plot.roc(obese, rf.model$votes[,1], percent=TRUE, col="#4daf4a", lwd=4, print.auc=TRUE, add=TRUE, print.auc.y=40)
    plot.roc(obese, glm.fit1$fitted.values, percent=TRUE, col="#b237b8", lwd=4, print.auc=TRUE, add=TRUE, print.auc.y=40)
    legend("bottomright", legend=c("Logisitic Regression", "Random Forest","ff"), col=c("#377eb8", "#4daf4a","#b237b8"), lwd=4)

Solution

  • There are indeed 3 ROC curves plotted in this plot.

    But curves 1 and 3 are exactly on top of each other. If you change the type of line (lty=2 to make it dotted) and increase the contrast (for instance making it red) you will see it appear. You can also move the AUC text (print.auc.y=30).

    plot.roc(obese, glm.fit1$fitted.values, percent=TRUE, col="red", lwd=2, lty=2, print.auc=TRUE, add=TRUE, print.auc.y=30)
    

    Because you sorted the weights, and because ROC curves only care about ranking of the data instead of the actual values, weight and weight1 are absolutely identical in ROC analysis.

    A plot of the 3 ROC curves