Home Â» How to Plot a ROC Curve Using ggplot2 (With Examples)

# How to Plot a ROC Curve Using ggplot2 (With Examples)

Logistic Regression is a statistical method that we use to fit a regression model when the response variable is binary.Â ToÂ assess how well a logistic regression model fits a dataset, we can look at the following two metrics:

• Sensitivity:Â The probability that the model predicts a positive outcome for an observation when indeed the outcome is positive.
• Specificity:Â TheÂ probability that the model predicts a negative outcome for an observation when indeed the outcome is negative.

One easy way to visualize these two metrics is by creating aÂ ROC curve, which is a plot that displays the sensitivity and specificity of a logistic regression model.

This tutorial explains how to create and interpret a ROC curve in R using the ggplot2 visualization package.

### Example: ROC Curve Using ggplot2

Suppose we fit the following logistic regression model in R:

```#load Default dataset from ISLR book
data #divide dataset into training and test set
set.seed(1)
sample TRUE, FALSE), nrow(data), replace=TRUE, prob=c(0.7,0.3))
train #fit logistic regression model to training set
model binomial", data=train)

#use model to make predictions on test set
predicted response")
```

To visualize how well the logistic regression model performs on the test set, we can create a ROC plot using theÂ ggroc() function from the pROC package:

```#load necessary packages
library(ggplot2)
library(pROC)

#define object to plot
rocobj
#create ROC plot
ggroc(rocobj)```

The y-axis displays the sensitivity (the true positive rate) of the model and the x-axis displays the specificity (the true negative rate) of the model.

Note that we can add some styling to the plot and also provide a title that contains the AUC (area under the curve) for the plot:

```#load necessary packages
library(ggplot2)
library(pROC)

#define object to plot and calculate AUC
rocobj round(auc(test\$default, predicted),4)

#create ROC plot
ggroc(rocobj, colour = 'steelblue', size = 2) +
ggtitle(paste0('ROC Curve ', '(AUC = ', auc, ')'))```

Note that we can also modify the theme of the plot:

```#create ROC plot with minimal theme
ggroc(rocobj, colour = 'steelblue', size = 2) +
ggtitle(paste0('ROC Curve ', '(AUC = ', auc, ')')) +
theme_minimal()```

Refer to this article for a guide to the best ggplot2 themes.