使用ggplot在R中绘制混淆矩阵

Har*_*hid 11 r ggplot2 confusion-matrix

我有两个混淆矩阵,计算值为真阳性(tp),假阳性(fp),真阴性(tn)和假阴性(fn),对应两种不同的方法.我想把它们表示为 在此输入图像描述

我相信facet grid或facet wrap可以做到这一点,但我发现很难开始.这是与method1和method2相对应的两个混淆矩阵的数据

dframe<-structure(list(label = structure(c(4L, 2L, 1L, 3L, 4L, 2L, 1L, 
3L), .Label = c("fn", "fp", "tn", "tp"), class = "factor"), value = c(9, 
0, 3, 1716, 6, 3, 6, 1713), method = structure(c(1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L), .Label = c("method1", "method2"), class = "factor")), .Names = c("label", 
"value", "method"), row.names = c(NA, -8L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

MYa*_*208 14

这可能是一个好的开始

library(ggplot2)
ggplot(data =  dframe, mapping = aes(x = label, y = method)) +
  geom_tile(aes(fill = value), colour = "white") +
  geom_text(aes(label = sprintf("%1.0f",value)), vjust = 1) +
  scale_fill_gradient(low = "white", high = "steelblue")
Run Code Online (Sandbox Code Playgroud)

编辑

TClass <- factor(c(0, 0, 1, 1))
PClass <- factor(c(0, 1, 0, 1))
Y      <- c(2816, 248, 34, 235)
df <- data.frame(TClass, PClass, Y)

library(ggplot2)
ggplot(data =  df, mapping = aes(x = TClass, y = PClass)) +
  geom_tile(aes(fill = Y), colour = "white") +
  geom_text(aes(label = sprintf("%1.0f", Y)), vjust = 1) +
  scale_fill_gradient(low = "blue", high = "red") +
  theme_bw() + theme(legend.position = "none")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


dat*_*ess 9

基于 MYaseen208 的答案的稍微模块化的解决方案。对于大型数据集/多项分类可能更有效:

confusion_matrix <- as.data.frame(table(predicted_class, actual_class))

ggplot(data = confusion_matrix
       mapping = aes(x = Var1,
                     y = Var2)) +
  geom_tile(aes(fill = Freq)) +
  geom_text(aes(label = sprintf("%1.0f", Freq)), vjust = 1) +
  scale_fill_gradient(low = "blue",
                      high = "red",
                      trans = "log") # if your results aren't quite as clear as the above example
Run Code Online (Sandbox Code Playgroud)


dav*_*dgd 6

这是另一个基于 ggplot2 的选项;首先是数据(来自插入符号):

library(caret)

# data/code from "2 class example" example courtesy of ?caret::confusionMatrix

lvs <- c("normal", "abnormal")
truth <- factor(rep(lvs, times = c(86, 258)),
                levels = rev(lvs))
pred <- factor(
  c(
    rep(lvs, times = c(54, 32)),
    rep(lvs, times = c(27, 231))),
  levels = rev(lvs))

confusionMatrix(pred, truth)
Run Code Online (Sandbox Code Playgroud)

并构建图(在设置“表”时根据需要替换您自己的矩阵):

library(ggplot2)
library(dplyr)

table <- data.frame(confusionMatrix(pred, truth)$table)

plotTable <- table %>%
  mutate(goodbad = ifelse(table$Prediction == table$Reference, "good", "bad")) %>%
  group_by(Reference) %>%
  mutate(prop = Freq/sum(Freq))

# fill alpha relative to sensitivity/specificity by proportional outcomes within reference groups (see dplyr code above as well as original confusion matrix for comparison)
ggplot(data = plotTable, mapping = aes(x = Reference, y = Prediction, fill = goodbad, alpha = prop)) +
  geom_tile() +
  geom_text(aes(label = Freq), vjust = .5, fontface  = "bold", alpha = 1) +
  scale_fill_manual(values = c(good = "green", bad = "red")) +
  theme_bw() +
  xlim(rev(levels(table$Reference)))
Run Code Online (Sandbox Code Playgroud)

选项1

# note: for simple alpha shading by frequency across the table at large, simply use "alpha = Freq" in place of "alpha = prop" when setting up the ggplot call above, e.g.,
ggplot(data = plotTable, mapping = aes(x = Reference, y = Prediction, fill = goodbad, alpha = Freq)) +
  geom_tile() +
  geom_text(aes(label = Freq), vjust = .5, fontface  = "bold", alpha = 1) +
  scale_fill_manual(values = c(good = "green", bad = "red")) +
  theme_bw() +
  xlim(rev(levels(table$Reference)))
Run Code Online (Sandbox Code Playgroud)

选项 2


Art*_*yom 5

这是一个非常古老的问题,但似乎仍然有一个非常直接的解决方案使用 ggplot2 尚未提及。

希望它可能对某人有帮助:

cm <- confusionMatrix(factor(y.pred), factor(y.test), dnn = c("Prediction", "Reference"))

plt <- as.data.frame(cm$table)
plt$Prediction <- factor(plt$Prediction, levels=rev(levels(plt$Prediction)))

ggplot(plt, aes(Prediction,Reference, fill= Freq)) +
        geom_tile() + geom_text(aes(label=Freq)) +
        scale_fill_gradient(low="white", high="#009194") +
        labs(x = "Reference",y = "Prediction") +
        scale_x_discrete(labels=c("Class_1","Class_2","Class_3","Class_4")) +
        scale_y_discrete(labels=c("Class_4","Class_3","Class_2","Class_1"))
Run Code Online (Sandbox Code Playgroud)

使用 ggplot2 的混淆矩阵图

  • 标签向后 (3认同)