[英]Plot a confusion matrix in r showing percentages (ggplot)
所以我有這兩列我想變成一個混淆矩陣
faceEmotion
neutral
sad
happy
disgusted
angry
fearful
neutral
sad
sad
happy
happy
fearful
和
faceEmotionGuess
neutral
sad
happy
disgusted
angry
fearful
sad
disgusted
happy
happy
sad
neutral
我看到了這個用 ggplot 制作的漂亮的混淆矩陣,但我不知道他是如何制作的。 谷歌並沒有太大幫助。
感謝您提供的任何幫助!
您的示例數據顯然不如創建所需圖像的示例數據豐富,但您可以從您的數據中創建類似的 plot,如下所示:
library(ggplot2)
tab <- table(df$faceEmotion, df$faceEmotionGuess)
tab <- tab / rowSums(tab)
tab <- as.data.frame(tab, stringsAsFactors = TRUE)
tab$Var2 <- factor(tab$Var2, rev(levels(tab$Var2)))
ggplot(tab, aes(Var1, Var2, fill = Freq)) +
geom_tile() +
geom_text(aes(label = scales::percent(Freq))) +
scale_fill_gradient(low = "white", high = "#3575b5") +
labs(x = "Emotion", y = "Guess", title = "Confusion matrix of emotions",
fill = "Select") +
theme(plot.title = element_text(size = 25, hjust = 0.5,
margin = margin(20, 0, 20, 0)),
legend.title = element_text(size = 14, margin = margin(0, 20, 10, 0)),
axis.title.x = element_text(margin = margin(20, 20, 20, 20), size = 18),
axis.title.y = element_text(margin = margin(0, 20, 0, 10), size = 18))
數據
df <- structure(list(faceEmotion = structure(c(5L, 6L, 4L, 2L, 1L,
3L, 5L, 6L, 6L, 4L, 4L, 3L), .Label = c("angry", "disgusted",
"fearful", "happy", "neutral", "sad"), class = "factor"),
faceEmotionGuess = structure(c(5L,
6L, 4L, 2L, 1L, 3L, 6L, 2L, 4L, 4L, 6L, 5L), .Label = c("angry",
"disgusted", "fearful", "happy", "neutral", "sad"), class = "factor")),
class = "data.frame", row.names = c(NA, -12L))
df
#> faceEmotion faceEmotionGuess
#> 1 neutral neutral
#> 2 sad sad
#> 3 happy happy
#> 4 disgusted disgusted
#> 5 angry angry
#> 6 fearful fearful
#> 7 neutral sad
#> 8 sad disgusted
#> 9 sad happy
#> 10 happy happy
#> 11 happy sad
#> 12 fearful neutral
由代表 package (v0.3.0) 於 2020 年 12 月 11 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.