如何在R中使用热图绘制混淆矩阵?

Bur*_*rcu 5 r heatmap confusion-matrix

我有一个混淆矩阵,使得:

  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 
Run Code Online (Sandbox Code Playgroud)

字母表示班级标签。

我只需要绘制混淆矩阵。我搜索了几个工具。R中的热图看起来像我所需要的。由于我对R一无所知,因此很难对样本进行更改。如果有人能尽快帮助我画画,我将不胜感激。或者也欢迎任何其他建议而不是热图。我知道有很多关于此的示例,但是仍然无法使用自己的数据进行绘制。

asa*_*ica 7

您可以使用 获得不错的结果ggplot2,但为此您需要一个包含 3 列 x、y 和要绘制的值的 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
Run Code Online (Sandbox Code Playgroud)

完美的!让我们开始绘图。带有 ggplot2 的热图的基本几何图形是geom_tile我们将提供的美学x,yfill.

library("ggplot2")
ggplot(hm, aes(x=x, y=y, fill=value)) + geom_tile() 
Run Code Online (Sandbox Code Playgroud)

第一次尝试热图

还不错,但我们可以做得更好。首先,我们可能想要反转 y 轴。诀窍是将 x 和 y 作为因子提供我们想要的级别。

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

然后我喜欢theme_bw()摆脱灰色背景的黑白主题。我也喜欢使用调色板RColorBrewerdirection = 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), ...
Run Code Online (Sandbox Code Playgroud)

更好的热图

画龙点睛:在瓷砖顶部打印值并删除图例,因为它不再有用。显然,这都是可选的,但它为您提供了构建材料。Notegeom_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
Run Code Online (Sandbox Code Playgroud)

最终热图

您还可以传递color="black"geom_tile以在图块周围绘制(黑色)线。带有RdYlBu配色方案的最终图(RColorBrewer::display.brewer.all()有关可用调色板的列表,请参阅参考资料)。

展示更多选择


csg*_*pie 3

正如格雷格提到的,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))
Run Code Online (Sandbox Code Playgroud)

您可能还想查看该heatmap函数:

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