How to Visualize Confusion Matrices with ggplot2
In Data Analytics Confusion Matrixis an important tool when evaluating the performance of a classification model. In this post, we'll use R's ggplot2 Packagesto visualize confusion matrices, as shown in the figure below, and how to visually represent each element of a confusion matrix to make it easier to understand.

Visualize Confusion Matrices with ggplot2

First, ggplot2is one of the most popular visualization libraries in R. It provides an intuitive way to represent confusion matrices. The code below creates a confusion matrix as a dataframe and passes it to the ggplot2to visualize confusion matrices, step by step.
1. load prerequisite libraries
To visualize the confusion matrix, we first need to load the ggplot2 library. ggplot2 is essential for many visualizations in R, and is a powerful tool for creating intuitive and stylish plots.
Load # prerequisite libraries
library(ggplot2)2. Generate confusion matrix data
The data representing the confusion matrix is stored in the Dataframesto create it. Set the predicted and actual values, and add a label (TP, FP, FN, TN) for each entry in the confusion matrix.
Create a data frame representing the # confusion matrix
confusion_data <- data.frame(
Predicted = factor(c("Y", "Y", "N", "N"), levels = c("N", "Y")), # predicted: set N and Y levels (N is below)
Actual = factor(c("Y", "N", "Y", "N"), levels = c("Y", "N")), # Actual: Set Y and N levels (Y is left)
Label = c("True Positive (TP)", "False Positive (FP)", "False Negative (FN)", "True Negative (TN)") # Label for each item
)Code Description:
- Predicted column is the value predicted by the model. Here, the
YandNwhere Y is positive and N is negative. - Actual column is the actual value, and the
YandNto use the - Labelare the labels corresponding to each entry in the confusion matrix, representing TP (true positive), FP (false positive), FN (false negative), and TN (true negative).
3. Visualize the confusion matrix with ggplot2
Now let's visualize the confusion matrix using ggplot2. geom_tile()to represent each item as a tile, and add a text label (TP, FP, FN, TN) to each tile.
# Visualize the confusion matrix using ggplot2
ggplot(confusion_data, aes(x = Actual, y = Predicted)) +
geom_tile(fill = "lightblue", color = "black", width = 1, height = 1) + # Create a tile, set color and border
geom_text(aes(label = Label), size = 5) + # Add a text label to the tile
labs(title = "Confusion Matrix", subtitle = "Actual (Reference)") + # Add title and subtitle
scale_x_discrete(position = "top") + # Place x-axis label at top
theme_minimal() + # Set a minimal theme
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold", margin = margin(b = 10)), # Set title
plot.subtitle = element_text(hjust = 0.5, size = 12, face = "bold", margin = margin(b = 20)), # Set subtitle
axis.title.x = element_blank(), # Remove x-axis title
axis.title.y = element_text(size = 12, face = "bold", margin = margin(r = 20)), # Set y-axis title
axis.text.x = element_text(size = 12, face = "bold", margin = margin(b = 10)), # Set x-axis label
axis.text.y = element_text(size = 12, face = "bold"), # Set y-axis label
panel.grid = element_blank(), # Remove grid
axis.ticks = element_blank(), # Remove axis tick marks
plot.margin = margin(t = 50, b = 40) # set margin
)Code Description:
- geom_tile()Draws each entry in the confusion matrix in tile format. We set the color of the tiles to light blue and the border to black.
- geom_text(): Display the labels (TP, FP, FN, TN) of the confusion matrix above each tile.
- labs(): Set the title and subtitle. In the subheading, add "Actual (Reference)" to indicate that the actual value is reference data.
- theme_minimal(): Set the background to white, and simplify the graph by removing unnecessary elements.
4. visualize the results and set the graph style
The result of visualizing the confusion matrix looks like the one at the beginning of the post. Each entry, shown in tiled format, has a True Positive, False Positive, False Negative, True Negativehas been added to help make it easier to understand what the confusion matrix means.
Final full code
Load # prerequisite libraries
library(ggplot2)
Create a data frame representing the # confusion matrix
confusion_data <- data.frame(
Predicted = factor(c("Y", "Y", "N", "N"), levels = c("N", "Y")), # predicted: set N and Y levels (N is below)
Actual = factor(c("Y", "N", "Y", "N"), levels = c("Y", "N")), # Actual: Set Y and N levels (Y is above)
Label = c("True Positive (TP)", "False Positive (FP)", "False Negative (FN)", "True Negative (TN)") # Labels for each item
)
# Visualize the confusion matrix using ggplot2
ggplot(confusion_data, aes(x = Actual, y = Predicted)) +
geom_tile(fill = "lightblue", color = "black", width = 1, height = 1) +
geom_text(aes(label = Label), size = 5) +
labs(title = "Confusion Matrix", subtitle = "Actual (Reference)") +
scale_x_discrete(position = "top") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold", margin = margin(b = 10)),
plot.subtitle = element_text(hjust = 0.5, size = 12, face = "bold", margin = margin(b = 20)),
axis.title.x = element_blank(),
axis.title.y = element_text(size = 12, face = "bold", margin = margin(r = 20)),
axis.text.x = element_text(size = 12, face = "bold", margin = margin(b = 10)),
axis.text.y = element_text(size = 12, face = "bold"),
panel.grid = element_blank(),
axis.ticks = element_blank(),
plot.margin = margin(t = 50, b = 40)
)Finalize
In this post, we'll utilize R's ggplot2 to plot the Confusion Matrix You've learned how to visualize it. The confusion matrix is an important tool to evaluate the performance of a classification model, and by visually representing the meaning of each entry (TP, FP, FN, TN), it is easy to understand how accurate the model is. This method makes analyzing confusion matrices more intuitive, and you can apply it to a variety of data!
To understand what confusion matrices mean for practical use, you need to understand the R Data Analysis Example: Learn classification and visualization with the Iris Iris dataset Review the post. You'll understand what we mean by checking the performance of your model, including accuracy, recall, and more.




