11.8 C
London
Friday, May 16, 2025
HomeTidyverse in Rggplot2 in RHow to Plot a ROC Curve Using ggplot2 (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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)

ROC curve in ggplot2

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, ')'))

ROC curve with AUC in ggplot2

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()

ROC curve in R using ggplot2

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories