Plotting ROC curve in R Programming (original) (raw)

Last Updated : 23 Jul, 2025

In binary classification problems, it's important to evaluate how well a model performs. One popular and useful method is using the ROC (Receiver Operating Characteristic) curve. This curve helps us visualize the trade-off between the model’s ability to correctly identify positive cases and the chance of incorrectly identifying negatives as positives.

What is an ROC Curve?

An ROC curve is a graph that shows the performance of a binary classifier as its decision threshold is changed. It plots:

Importance of ROC Curves in Model Evaluation

The ROC curve in R helps in understanding how well the model performs across different thresholds. It provides a visual understanding of the trade-off between true positives and false positives. The ROC curve is particularly helpful when:

To work with ROC curves in R, we can use two packages:

R `

install.packages("pROC") install.packages("ROCR")

`

1. Plotting ROC Curve Using pROC

The pROC package makes it simple to compute and visualize ROC curves. Let's start with a basic example using a simulated dataset.

set.seed(123) actual <- sample(c(0, 1), 100, replace = TRUE)
predicted_probs <- runif(100)

library(pROC)

roc_curve <- roc(actual, predicted_probs)

plot(roc_curve, col = "blue", main = "ROC Curve", print.auc = TRUE)

abline(a = 0, b = 1, lty = 2, col = "red")

`

**Output:

gh

Plotting ROC curve in R Programming

In this graph

2. Plotting ROC Curve Using ROCR

The ROCR package offers flexibility in terms of plotting and evaluating the ROC curve with more customizable options.

library(ROCR)

pred <- prediction(predicted_probs, actual)

perf <- performance(pred, "tpr", "fpr")

plot(perf, col = "darkgreen", lwd = 2, main = "ROC Curve with ROCR")

abline(a = 0, b = 1, col = "red", lty = 2)

`

**Output:

gh

Plotting ROC curve in R Programming

In this graph