Skip to contents

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.

Usage

pretty_cm(
  cm,
  color_grad = c(alpha("yellowgreen", 0.4), alpha("springgreen3", 0.85)),
  midpoint = 50,
  hide_zero = FALSE,
  ord = NA,
  diag = c("r", "reverse"),
  tile = c("both", "b", "prop", "p", "freq", "f"),
  tile_size = 3.5,
  tile_nod = 1,
  plot = TRUE
)

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?

Value

ggplot2 object - visualization of the confusion matrix.

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 title

  • axis.text.x = element_text(size=12) to change axis ticks (description labels)

See also

confusionMatrix theme

Author

Björn Büdenbender

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