我正在try 制作一个混淆矩阵,用以下数据显示面部表情的正确/不正确猜测:

> dput(conf_mat)
structure(list(Target = c("Angry", "Angry", "Angry", "Angry", 
"Angry", "Angry", "Angry", "Disgusted", "Disgusted", "Disgusted", 
"Disgusted", "Disgusted", "Disgusted", "Disgusted", "Fearful", 
"Fearful", "Fearful", "Fearful", "Fearful", "Fearful", "Fearful", 
"Happy", "Happy", "Happy", "Happy", "Happy", "Happy", "Happy", 
"Neutral", "Neutral", "Neutral", "Neutral", "Neutral", "Neutral", 
"Neutral", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Surprised", 
"Surprised", "Surprised", "Surprised", "Surprised", "Surprised", 
"Surprised"), Prediction = c("Angry", "Disgusted", "Fearful", 
"Happy", "Neutral", "Sad", "Surprised", "Angry", "Disgusted", 
"Fearful", "Happy", "Neutral", "Sad", "Surprised", "Angry", "Disgusted", 
"Fearful", "Happy", "Neutral", "Sad", "Surprised", "Angry", "Disgusted", 
"Fearful", "Happy", "Neutral", "Sad", "Surprised", "Angry", "Disgusted", 
"Fearful", "Happy", "Neutral", "Sad", "Surprised", "Angry", "Disgusted", 
"Fearful", "Happy", "Neutral", "Sad", "Surprised", "Angry", "Disgusted", 
"Fearful", "Happy", "Neutral", "Sad", "Surprised"), N = c(456L, 
31L, 14L, 1L, 11L, 46L, 1L, 92L, 454L, 3L, 2L, 1L, 4L, 4L, 2L, 
40L, 382L, 1L, 1L, 10L, 124L, 0L, 2L, 0L, 552L, 3L, 2L, 1L, 3L, 
2L, 2L, 7L, 528L, 16L, 2L, 8L, 30L, 17L, 4L, 19L, 481L, 1L, 0L, 
4L, 20L, 3L, 2L, 4L, 527L)), row.names = c(NA, -49L), class = c("tbl_df", 
"tbl", "data.frame"))

根据在线教程,我能够获得:

library(cvms)
plot_confusion_matrix(conf_mat,
                      class_order=c("Surprised", "Disgusted", "Fearful", "Angry",  "Sad", "Happy", "Neutral"),
                      add_counts=FALSE,
                      add_row_percentages=FALSE,
                      add_col_percentages=FALSE
                      )

enter image description here

上图显示了每个单元在整个数据集中的出现频率.我希望它显示每个预测在各个列中的普遍性如何,以便每列的总和为1.如何做到这一点(不切换到Python)?这是我正在寻找的示例(来自其他人的作品;请注意,轴是翻转的,因此在下面的照片中,行和等于1,而不是列):

enter image description here

推荐答案

您可以计算出N乘以Target的比例:

library(ggplot2)
library(dplyr)

lvl <- c("Surprised", "Disgusted", "Fearful", "Angry",  "Sad", "Happy", "Neutral")

conf_mat |>
  mutate(Target = factor(Target, levels = rev(lvl)),
         Prediction = factor(Prediction, levels = lvl)) |>
  mutate(prop = N / sum(N),
         prop_col = if_else(prop > .5, "white", "black"), .by = Target) |>
  ggplot(aes(x = Target, y = Prediction, fill = prop)) +
  geom_tile() +
  geom_text(aes(label = scales::label_percent(.1)(prop), color = prop_col)) +
  scale_fill_gradient(low = "#EEF2F8", high = scales::muted("blue"), guide = "none") +
  scale_color_identity() +
  scale_x_discrete(position = "top") +
  theme_minimal()

enter image description here

R相关问答推荐

如何创建具有总计列和ggplot 2所有条线的百分比标签的堆叠条形图?

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

将Multilinetring合并到一个线串中,使用sf生成规则间隔的点

从有序数据中随机抽样

编辑文件后编辑RhandsonTable

在数学中正确显示摄氏度、开氏度或华氏度

在R中为马赛克图中的每个字段着色

线性模型斜率在减少原始数据时提供NA

在GG图中绘制射线的自动程序

如何在R中改变fviz_pca_biplot中圆的边界线的 colored颜色 ?

在使用具有Bray-Curtis相似性的pvCluust时计算p值

如何将一些单元格的内容随机 Select 到一个数据框中?

根据r中另一个文本列中给定的范围对各列求和

层次树图的数据树

创建在文本字符串中发现两个不同关键字的实例的数据框

Conditional documentr::R中数据帧的summarize()

在不重复主题的情况下重新排列组

如何编辑被动式数据表?

在R中添加要打印的垂直线

在一个multiplot中以非对称的方式在R中绘制多个图