ggplot2混淆矩阵条件填充

时间:2018-07-09 16:38:19

标签: r ggplot2 r-caret confusion-matrix

我希望混淆矩阵具有两个不同的尺度。对于真正的积极/消极,我想绿色代表高,红色代表低。对于错误的结果,我希望颜色反转。这可能需要两个传说吗?解释起来可能很麻烦,但是我觉得倒置的颜色更直观。您的真实结果应该很高(绿色代表良好),而负面结果则应该较低(绿色代表良好)。

如果除了我所建议的以外,还有其他建议或最佳做法,我愿意接受其他选择。

library(caret)
library(tidyverse)
data("GermanCredit")
cm <- confusionMatrix(GermanCredit$Class, sample(GermanCredit$Class))

cm$table %>%
  data.frame() %>% 
  mutate(Prediction = factor(Prediction, levels = c("Good", "Bad"))) %>%
  group_by(Reference) %>% 
  mutate(total = sum(Freq)) %>% 
  ungroup() %>% 
  ggplot(aes(Reference, Prediction, fill = Freq)) +
  geom_tile() +
  geom_text(aes(label = Freq), size = 8) +
  scale_fill_gradient(low = "#ea4434", high = "#badb33") +
  scale_x_discrete(position = "top") +
  geom_tile(color = "black", fill = "black", alpha = 0)

基于一些评论,我添加了我的问题:我希望不相关的对角线为白色,而红色(低)和绿色(高)比例仅适用于真实的正/负结果。例如,下面的图部分实现了这一点,除了我将低点设置为白色...我希望低点为红色,而白色方块保持白色。

library(reprex)
library(caret)
#> Loading required package: lattice
#> Loading required package: ggplot2
library(tidyverse)
data("GermanCredit")
cm <- confusionMatrix(GermanCredit$Class, sample(GermanCredit$Class))

cm$table %>%
  data.frame() %>% 
  mutate(Prediction = factor(Prediction, levels = c("Good", "Bad"))) %>%
  group_by(Reference) %>% 
  mutate(
    total = sum(Freq),
    frac_fill = if_else(Prediction == Reference, Freq / total, 0),
    frac = Freq / total * frac_fill
  ) %>%
  ggplot(aes(Prediction, Reference, fill = frac_fill)) +
  geom_tile() +
  geom_text(aes(label = str_c(Freq, ", ", round(frac * 100), "%")), size = 8) +
  scale_fill_gradient(low = "white", high = "#badb33") +
  scale_x_discrete(position = "top") +
  geom_tile(color = "black", fill = "black", alpha = 0)

2 个答案:

答案 0 :(得分:1)

如果我的理解正确,您想要一个发散的比例,这需要使比例之一为负,而比例为正。如果预测为“不良”,我将变量frac_directed设为frac_fill的负数,尽管这可能与您要查找的相反。然后使用scale_fill_gradient2代替scale_fill_gradient,您可以设置一个中点(默认为0,此处合适)和中间填充值。

library(caret)
library(tidyverse)
data("GermanCredit")
cm <- confusionMatrix(GermanCredit$Class, sample(GermanCredit$Class))

cm$table %>%
  data.frame() %>% 
  mutate(Prediction = factor(Prediction, levels = c("Good", "Bad"))) %>%
  group_by(Reference) %>% 
  mutate(
    total = sum(Freq),
    frac_fill = if_else(Prediction == Reference, Freq / total, 0),
    frac = Freq / total * frac_fill
  ) %>%
  mutate(frac_directed = if_else(Prediction == "Bad", frac_fill * -1, frac_fill)) %>%
  ggplot(aes(Prediction, Reference, fill = frac_directed)) +
  geom_tile(color = "black") +
  geom_text(aes(label = str_c(Freq, ", ", round(frac * 100), "%")), size = 8) +
  scale_fill_gradient2(low = "red", mid = "white", high = "#badb33") +
  scale_x_discrete(position = "top")

reprex package(v0.2.0)于2018-07-09创建。

答案 1 :(得分:0)

好,这是一种尝试,主要是对非对角线图块使用反向填充值。我将轴颠倒了,以至于我通常认为是混淆矩阵。我猜这表明,该模型比Good更能预测Bad,但我认为当混淆时,它掩盖了实际混淆的内容。当这样呈现时,传说也不是最有用的信息,我什至可以不去理会。

library(caret)
#> Loading required package: lattice
#> Loading required package: ggplot2
library(tidyverse)
data("GermanCredit")
cm <- confusionMatrix(GermanCredit$Class, sample(GermanCredit$Class))

cm$table %>%
  data.frame() %>% 
  mutate(Prediction = factor(Prediction, levels = c("Good", "Bad"))) %>%
  group_by(Reference) %>% 
  mutate(
    total = sum(Freq),
    frac = Freq / total,
    frac_fill = if_else(Prediction == Reference, Freq / total, 1 - (Freq / total))
    ) %>%
  ggplot(aes(Prediction, Reference, fill = frac_fill)) +
  geom_tile() +
  geom_text(aes(label = str_c(Freq, ", ", round(frac * 100), "%")), size = 8) +
  scale_fill_gradient(low = "#ea4434", high = "#badb33") +
  scale_x_discrete(position = "top") +
  geom_tile(color = "black", fill = "black", alpha = 0)

reprex package(v0.2.0)于2018-07-09创建。