rplotrocproc-r-package

How to combine several roc curves in one graph in R?


I draw 4 roc curves for 4 models, and want to put them together in one graph so that I can compare.

library(pROC)

lr.probs <- predict(lr_model, newdata=test1, type='response')  
lr.plot <- plot(roc(test1$Y,lr.probs))  
gbm.probs <- predict(gbm,test1\[,predictorNames0\],type="prob")
gbm.plot <-plot(roc(test1$Y,gbm.probs\[,2\]))  
rf.probs <- predict(rf,test1\[,predictorNames0\],type="prob")
rf.plot <-plot(roc(test1$Y,rf.probs\[,2\]), col="blue")  
xgb.probs <- predict(model_xgb, newdata=d_test, type='response')
xgb.plot <-  plot(roc(test2$Y,xgb.probs))

it gives me 4 indivudual graphs now


Solution

  • You can either use the plot function with the add=TRUE argument:

    lr.plot <- plot(roc(test1$Y,lr.probs))  
    gbm.plot <-plot(roc(test1$Y,gbm.probs\[,2\]), add=TRUE, col="red")  
    rf.plot <-plot(roc(test1$Y,rf.probs\[,2\]), add=TRUE, col="blue")  
    xgb.plot <-  plot(roc(test2$Y,xgb.probs), add=TRUE, col="green")
    

    Alternatively you can use the lines function:

    lr.plot <- plot(roc(test1$Y,lr.probs))  
    gbm.plot <-lines(roc(test1$Y,gbm.probs\[,2\]), col="red")  
    rf.plot <-lines(roc(test1$Y,rf.probs\[,2\]), col="blue")  
    xgb.plot <-  lines(roc(test2$Y,xgb.probs), col="green")