plot_confusion_matrix {cvms} | R Documentation |
Creates a ggplot2
object representing a confusion matrix with counts,
overall percentages, row percentages and column percentages. An extra row and column with sum tiles and the
total count can be added.
The confusion matrix can be created with evaluate()
. See `Examples`
.
While this function is intended to be very flexible (hence the large number of arguments),
the defaults should work in most cases for most users. See the Examples
.
plot_confusion_matrix(
conf_matrix,
target_col = "Target",
prediction_col = "Prediction",
counts_col = "N",
class_order = NULL,
add_sums = FALSE,
add_counts = TRUE,
add_normalized = TRUE,
add_row_percentages = TRUE,
add_col_percentages = TRUE,
diag_percentages_only = FALSE,
rm_zero_percentages = TRUE,
rm_zero_text = TRUE,
add_zero_shading = TRUE,
add_arrows = TRUE,
counts_on_top = FALSE,
palette = "Blues",
intensity_by = "counts",
theme_fn = ggplot2::theme_minimal,
place_x_axis_above = TRUE,
rotate_y_text = TRUE,
digits = 1,
font_counts = font(),
font_normalized = font(),
font_row_percentages = font(),
font_col_percentages = font(),
arrow_size = 0.048,
arrow_nudge_from_text = 0.065,
tile_border_color = NA,
tile_border_size = 0.1,
tile_border_linetype = "solid",
sums_settings = sum_tile_settings(),
darkness = 0.8
)
conf_matrix |
Confusion matrix E.g. for a binary classification:
As created with the various evaluation functions in Note: If you supply the results from | ||||||||||||||||
target_col |
Name of column with target levels. | ||||||||||||||||
prediction_col |
Name of column with prediction levels. | ||||||||||||||||
counts_col |
Name of column with a count for each combination of the target and prediction levels. | ||||||||||||||||
class_order |
Names of the classes in | ||||||||||||||||
add_sums |
Add tiles with the row/column sums. Also adds a total count tile. (Logical) The appearance of these tiles can be specified in Note: Adding the sum tiles with a palette requires the | ||||||||||||||||
add_counts |
Add the counts to the middle of the tiles. (Logical) | ||||||||||||||||
add_normalized |
Normalize the counts to percentages and add to the middle of the tiles. (Logical) | ||||||||||||||||
add_row_percentages |
Add the row percentages, i.e. how big a part of its row the tile makes up. (Logical) By default, the row percentage is placed to the right of the tile, rotated 90 degrees. | ||||||||||||||||
add_col_percentages |
Add the column percentages, i.e. how big a part of its column the tile makes up. (Logical) By default, the row percentage is placed at the bottom of the tile. | ||||||||||||||||
diag_percentages_only |
Whether to only have row and column percentages in the diagonal tiles. (Logical) | ||||||||||||||||
rm_zero_percentages |
Whether to remove row and column percentages when the count is | ||||||||||||||||
rm_zero_text |
Whether to remove counts and normalized percentages when the count is | ||||||||||||||||
add_zero_shading |
Add image of skewed lines to zero-tiles. (Logical) Note: Adding the zero-shading requires the | ||||||||||||||||
add_arrows |
Add the arrows to the row and col percentages. (Logical) Note: Adding the arrows requires the | ||||||||||||||||
counts_on_top |
Switch the counts and normalized counts, such that the counts are on top. (Logical) | ||||||||||||||||
palette |
Color scheme. Passed directly to Try these palettes: | ||||||||||||||||
intensity_by |
The measure that should control the color intensity of the tiles.
Either | ||||||||||||||||
theme_fn |
The | ||||||||||||||||
place_x_axis_above |
Move the x-axis text to the top and reverse the levels such that the "correct" diagonal goes from top left to bottom right. (Logical) | ||||||||||||||||
rotate_y_text |
Whether to rotate the y-axis text to be vertical instead of horizontal. (Logical) | ||||||||||||||||
digits |
Number of digits to round to (percentages only). Set to a negative number for no rounding. Can be set for each font individually via the | ||||||||||||||||
font_counts |
| ||||||||||||||||
font_normalized |
| ||||||||||||||||
font_row_percentages |
| ||||||||||||||||
font_col_percentages |
| ||||||||||||||||
arrow_size |
Size of arrow icons. (Numeric) Is divided by | ||||||||||||||||
arrow_nudge_from_text |
Distance from the percentage text to the arrow. (Numeric) | ||||||||||||||||
tile_border_color |
Color of the tile borders. Passed as | ||||||||||||||||
tile_border_size |
Size of the tile borders. Passed as | ||||||||||||||||
tile_border_linetype |
Linetype for the tile borders. Passed as | ||||||||||||||||
sums_settings |
A list of settings for the appearance of the sum tiles.
Can be provided with | ||||||||||||||||
darkness |
How dark the darkest colors should be, between Technically, a lower value increases the upper limit in
|
Inspired by Antoine Sachet's answer at https://stackoverflow.com/a/53612391/11832955
A ggplot2
object representing a confusion matrix.
Color intensity depends on either the counts (default) or the overall percentages.
By default, each tile has the normalized count (overall percentage) and count in the middle, the column percentage at the bottom, and the row percentage to the right and rotated 90 degrees.
In the "correct" diagonal (upper left to bottom right, by default), the column percentages are the class-level sensitivity scores, while the row percentages are the class-level positive predictive values.
Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk
Other plotting functions:
font()
,
plot_metric_density()
,
plot_probabilities_ecdf()
,
plot_probabilities()
,
sum_tile_settings()
# Attach cvms
library(cvms)
library(ggplot2)
# Two classes
# Create targets and predictions data frame
data <- data.frame(
"target" = c("A", "B", "A", "B", "A", "B", "A", "B",
"A", "B", "A", "B", "A", "B", "A", "A"),
"prediction" = c("B", "B", "A", "A", "A", "B", "B", "B",
"B", "B", "A", "B", "A", "A", "A", "A"),
stringsAsFactors = FALSE
)
# Evaluate predictions and create confusion matrix
eval <- evaluate(
data = data,
target_col = "target",
prediction_cols = "prediction",
type = "binomial"
)
# Inspect confusion matrix tibble
eval[["Confusion Matrix"]][[1]]
# Plot confusion matrix
# Supply confusion matrix tibble directly
plot_confusion_matrix(eval[["Confusion Matrix"]][[1]])
# Plot first confusion matrix in evaluate() output
plot_confusion_matrix(eval)
# Add sum tiles
plot_confusion_matrix(eval, add_sums = TRUE)
# Three (or more) classes
# Create targets and predictions data frame
data <- data.frame(
"target" = c("A", "B", "C", "B", "A", "B", "C",
"B", "A", "B", "C", "B", "A"),
"prediction" = c("C", "B", "A", "C", "A", "B", "B",
"C", "A", "B", "C", "A", "C"),
stringsAsFactors = FALSE
)
# Evaluate predictions and create confusion matrix
eval <- evaluate(
data = data,
target_col = "target",
prediction_cols = "prediction",
type = "multinomial"
)
# Inspect confusion matrix tibble
eval[["Confusion Matrix"]][[1]]
# Plot confusion matrix
# Supply confusion matrix tibble directly
plot_confusion_matrix(eval[["Confusion Matrix"]][[1]])
# Plot first confusion matrix in evaluate() output
plot_confusion_matrix(eval)
# Add sum tiles
plot_confusion_matrix(eval, add_sums = TRUE)
# Counts only
plot_confusion_matrix(
eval[["Confusion Matrix"]][[1]],
add_normalized = FALSE,
add_row_percentages = FALSE,
add_col_percentages = FALSE
)
# Change color palette to green
# Change theme to \code{theme_light}.
plot_confusion_matrix(
eval[["Confusion Matrix"]][[1]],
palette = "Greens",
theme_fn = ggplot2::theme_light
)
# The output is a ggplot2 object
# that you can add layers to
# Here we change the axis labels
plot_confusion_matrix(eval[["Confusion Matrix"]][[1]]) +
ggplot2::labs(x = "True", y = "Guess")