簡體   English   中英

如何使用 R 中的熱圖繪制混淆矩陣?

[英]How to plot a confusion matrix using heatmaps in R?

我有一個混淆矩陣,這樣:

  a b c d e f g h i j
a 5 4 0 0 0 0 0 0 0 0
b 0 0 0 0 0 0 0 0 0 0
c 0 0 4 0 0 0 0 0 0 0
d 0 0 0 0 0 0 0 0 0 0
e 2 0 0 0 2 0 0 0 0 0
f 1 0 0 0 0 2 0 0 0 0
g 0 0 0 0 0 0 0 0 0 0
h 0 0 0 0 0 0 0 0 0 0 
i 0 0 0 0 0 0 0 0 0 0 
j 0 0 0 0 0 0 0 0 0 0 

其中字母表示類標簽。

我只需要繪制混淆矩陣。 我搜索了幾個工具。 R 中的熱圖看起來像我需要的。 由於我對 R 一無所知,因此很難對樣本進行更改。 如果有人能很快幫助我如何畫畫,我將不勝感激。 或者也歡迎任何其他建議而不是熱圖。 我知道有很多關於此的樣本,但我仍然無法使用自己的數據進行繪制。

您可以使用ggplot2獲得不錯的結果,但為此您需要一個包含 x、y 和要繪制的值的 3 列的 data.frame。

利用gathertidyr工具也很容易重新格式化您的數據:

library("dplyr")
library("tidyr")

# Loading your example. Row names should get their own column (here `y`).
hm <- readr::read_delim("y a b c d e f g h i j
a 5 4 0 0 0 0 0 0 0 0
b 0 0 0 0 0 0 0 0 0 0
c 0 0 4 0 0 0 0 0 0 0
d 0 0 0 0 0 0 0 0 0 0
e 2 0 0 0 2 0 0 0 0 0
f 1 0 0 0 0 2 0 0 0 0
g 0 0 0 0 0 0 0 0 0 0
h 0 0 0 0 0 0 0 0 0 0
i 0 0 0 0 0 0 0 0 0 0
j 0 0 0 0 0 0 0 0 0 0", delim=" ")

# Gathering columns a to j
hm <- hm %>% gather(x, value, a:j)

# hm now looks like:
# # A tibble: 100 x 3
# y     x     value
# <chr> <chr> <dbl>
# 1 a     a         5
# 2 b     a         0
# 3 c     a         0
# 4 d     a         0
# 5 e     a         2
# # ... with 95 more rows

完美的! 讓我們開始繪圖。 帶有 ggplot2 的熱圖的基本幾何圖形是geom_tile ,我們將為其提供美觀的xyfill

library("ggplot2")
ggplot(hm, aes(x=x, y=y, fill=value)) + geom_tile() 

第一次嘗試熱圖

還不錯,但我們可以做得更好。 首先,我們可能想要反轉 y 軸。 訣竅是將 x 和 y 作為因子提供我們想要的級別。

hm <- hm %>%
  mutate(x = factor(x), # alphabetical order by default
         y = factor(y, levels = rev(unique(y)))) # force reverse alphabetical order

然后我喜歡擺脫灰色背景的黑白主題theme_bw() 我還喜歡使用來自RColorBrewer的調色板( direction = 1以獲得更高值的更深顏色)。

由於您在xy軸上繪制相同的內容,因此您可能需要相等的軸比例: coord_equal()將為您提供一個方形圖。

ggplot(hm, aes(x=x, y=y, fill=value)) +
  geom_tile() + theme_bw() + coord_equal() +
  scale_fill_distiller(palette="Greens", direction=1) 
# Other valid palettes: Reds, Blues, Spectral, RdYlBu (red-yellow-blue), ...

更好的熱圖

畫龍點睛:在瓷磚頂部打印值並刪除圖例,因為它不再有用。 顯然,這都是可選的,但它為您提供了構建材料。 注意geom_text繼承了xy美學,因為它們被傳遞給ggplot

ggplot(hm, aes(x=x, y=y, fill=value)) +
  geom_tile() + theme_bw() + coord_equal() +
  scale_fill_distiller(palette="Greens", direction=1) +
  guides(fill=F) + # removing legend for `fill`
  labs(title = "Value distribution") + # using a title instead
  geom_text(aes(label=value), color="black") # printing values

最終熱圖

您還可以將color="black"傳遞給geom_tile以在瓷磚周圍繪制(黑色)線。 使用RdYlBu配色方案的最終繪圖RColorBrewer::display.brewer.all()有關可用調色板的列表,請參閱RColorBrewer::display.brewer.all() )。

展示更多選擇

正如格雷格提到的, image可能是要走的路:

z = c(5,4,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,4,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
2,0,0,0,2,0,0,0,0,0,
1,0,0,0,0,2,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0)

z = matrix(z, ncol=10)
colnames(z) = c("a","b","c","d","e","f","g","h","i", "j")
rownames(z) = c("a","b","c","d","e","f","g","h","i", "j")

##To get the correct image plot rotation
##We need to flip the plot
image(z[,ncol(z):1], axes=FALSE)

##Add in the y-axis labels. Similar idea for x-axis.
axis(2, at = seq(0, 1, length=length(colnames(z))), labels=colnames(z))

您可能還想查看heatmap功能:

heatmap(t(z)[ncol(z):1,], Rowv=NA,
               Colv=NA, col = heat.colors(256))

R 中的image函數將采用一個矩陣並根據矩陣中的值繪制一個帶有顏色的規則網格。 您可以設置很多選項,但只需將您的矩陣作為唯一參數調用 image 將創建一個基本圖。 聽起來這將是一個很好的起點。

不幸的是,另一個答案中建議的image函數不能這樣使用,因為它反轉(鏡像)數據,所以你會以錯誤的方式得到它。 通過一點點變換,您可以創建一個可以正確繪制它的函數:

set.seed(1)
d = data.frame(Y_label=rpois(100,1), pred=rpois(100,1))
Show = function(df, ...) {image(t(df[nrow(df):1,]), ...)}
Show(table(d), main="my confusion matrix")

在此處輸入圖片說明

下一步您可以添加一些軸標簽,自定義它等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM