简体   繁体   English

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

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

I have a confusion matrix such that:我有一个混淆矩阵,这样:

  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 

where the letters denote the class labels.其中字母表示类标签。

I just need to plot the confusion matrix.我只需要绘制混淆矩阵。 I searched a couple of tools.我搜索了几个工具。 Heatmaps in R looks like what I need. R 中的热图看起来像我需要的。 As I don't know anything about R, it is really hard to do changes on the samples.由于我对 R 一无所知,因此很难对样本进行更改。 If anybody could help me shortly how to draw, I will be really appreciated.如果有人能很快帮助我如何画画,我将不胜感激。 Or any other suggestion rather than heatmaps are welcome as well.或者也欢迎任何其他建议而不是热图。 I know there is plenty of samples about this, but still I cannot manage to draw with my own data.我知道有很多关于此的样本,但我仍然无法使用自己的数据进行绘制。

You can achieve a nice result using ggplot2 , but for that you need a data.frame with 3 columns for x, y and the value to plot.您可以使用ggplot2获得不错的结果,但为此您需要一个包含 x、y 和要绘制的值的 3 列的 data.frame。

Using gather from the tidyr tool it is very easy to reformat your data:利用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

Perfect!完美的! Let's get plotting.让我们开始绘图。 the basic geom for heatmap with ggplot2 is geom_tile to which we'll provide aesthetic x , y and fill .带有 ggplot2 的热图的基本几何图形是geom_tile ,我们将为其提供美观的xyfill

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

第一次尝试热图

OK not too bad but we can do much better.还不错,但我们可以做得更好。 First we probably want to reverse the y axis.首先,我们可能想要反转 y 轴。 The trick is to provide x and y as factors with the levels ordered as we want them.诀窍是将 x 和 y 作为因子提供我们想要的级别。

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

Then I like the black & white theme theme_bw() which gets rid of the grey background.然后我喜欢摆脱灰色背景的黑白主题theme_bw() I also like to use a palette from RColorBrewer (with direction = 1 to get the darker colors for higher values).我还喜欢使用来自RColorBrewer的调色板( direction = 1以获得更高值的更深颜色)。

Since you're plotting the same thing on the x and y axis, you probably want equal axis scales: coord_equal() will give you a square plot.由于您在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), ...

更好的热图

The finishing touch: printing the values on top of the tiles and removing the legend since it is not longer useful.画龙点睛:在瓷砖顶部打印值并删除图例,因为它不再有用。 Obviously this is all optional but it gives you material to build from.显然,这都是可选的,但它为您提供了构建材料。 Note geom_text inherits the x and y aesthetics since they were passed to ggplot .注意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

最终热图

You could also pass color="black" to geom_tile to draw (black) lines around the tiles.您还可以将color="black"传递给geom_tile以在瓷砖周围绘制(黑色)线。 A final plot with the RdYlBu color scheme (see RColorBrewer::display.brewer.all() for a list of available palettes).使用RdYlBu配色方案的最终绘图RColorBrewer::display.brewer.all()有关可用调色板的列表,请参阅RColorBrewer::display.brewer.all() )。

展示更多选择

As Greg mentioned, image is probably the way to go:正如格雷格提到的, 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))

You may also want to look at the heatmap function:您可能还想查看heatmap功能:

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

The image function in R will take a matrix and plot a regular grid with colors based on the values in the matrix. R 中的image函数将采用一个矩阵并根据矩阵中的值绘制一个带有颜色的规则网格。 You can set a lot of options, but just calling image with your matrix as the only argument will create a basic plot.您可以设置很多选项,但只需将您的矩阵作为唯一参数调用 image 将创建一个基本图。 Sounds like that would be a good place to start.听起来这将是一个很好的起点。

Unfortunately, the image function suggested in another answer cannot be used as such because it reverses (mirror) the data, so you'll get it the wrong way.不幸的是,另一个答案中建议的image函数不能这样使用,因为它反转(镜像)数据,所以你会以错误的方式得到它。 With a little transform you can coin a function that will plot it right:通过一点点变换,您可以创建一个可以正确绘制它的函数:

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")

在此处输入图片说明

Next step you can add some axis labels, customize it, etc.下一步您可以添加一些轴标签,自定义它等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM