我有一个混淆矩阵,这样:
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一无所知,所以真的很难对样本进行更改。如果有人能在短时间内帮助我如何绘画,我将不胜感激。或者任何其他建议,而不是热图也是受欢迎的。我知道有很多关于这方面的样本,但我仍然不能用我自己的数据来绘制。
发布于 2011-09-15 04:30:02
正如格雷格提到的,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))发布于 2018-12-04 19:53:04
使用ggplot2可以获得很好的结果,但为此您需要一个包含3列x,y和要绘制的值的data.frame。
从tidyr工具中使用gather可以非常容易地重新格式化数据:
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() )。

发布于 2011-09-15 03:16:48
R中的image函数将获取一个矩阵,并根据矩阵中的值用颜色绘制一个规则网格。您可以设置许多选项,但仅使用矩阵作为唯一参数调用image将创建一个基本的图。听起来这将是一个很好的起点。
https://stackoverflow.com/questions/7421503
复制相似问题