[英]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。
利用gather
自tidyr
工具也很容易重新格式化您的數據:
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
,我們將為其提供美觀的x
、 y
和fill
。
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
以獲得更高值的更深顏色)。
由於您在x
和y
軸上繪制相同的內容,因此您可能需要相等的軸比例: 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
繼承了x
和y
美學,因為它們被傳遞給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 將創建一個基本圖。 聽起來這將是一個很好的起點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.