Takes a confusion matrix (either a data.frame, table or an
confusionMatrix
object and plots a nice visualization.
Thanks to Felicitas Kininger for inspiring the inclusion of this function
into the package.
Arguments
- cm
Either a
confusionMatrix
, a table or a data.frame, with prediction as the the rows and reference as the columns (mandatory parameter)- color_grad
Pole of color gradient to use for the tiles, Default: c(alpha("yellowgreen", 0.4), alpha("springgreen3", 0.85))
- midpoint
Numeric, Default = 50; Manually setting a middle point in percentage for the color scale.
- hide_zero
Hide tiles with 0 percentage, Default: FALSE
- ord
Character, Default = NA; Order of the factor levels to display (if you want to change it manually for the plot).
- diag
Orientation of the diagonal (sensitivities), possible values diag = "r" or "reverse"
- tile
Character, Default = "both"; Either "p" or "prop" for proportion | "f" or "freq" for frequency | "b" or "both" for both. If character is not recognized or missing it goes to "both".
- tile_size
Numeric, Default = 3.5; Determines the size of the font in the tiles. Be wary, other scale than for usual font size.
- tile_nod
Numeric (or NA), Default = 1; Determines the number of decimals to be displayed in case tiles should show percentages "p".
- plot
Logical, Default = TRUE; Shall the output also be plotted?
Details
You can change all fonts of the plot later on with
theme
. Use the following inside the call to theme
theme(axis.title.x = element_text(size=14))
to change axis titleaxis.text.x = element_text(size=12)
to change axis ticks (description labels)
See also
confusionMatrix
theme
Examples
if (FALSE) {
if (interactive()) {
# Creating random example data: prediction of neural network on content
# of animal pictures
set.seed(23)
pred <- factor(sample(c("dog", "cat"), 100, replace = TRUE))
ref <- factor(sample(c("dog", "cat"), 100, replace = TRUE))
cm <- caret::confusionMatrix(pred, ref)
# Plotting of the caret confusion matrix
pretty_cm(cm)
}
}