In R, is there a similar function to heatmap() to visualize groups in a large matrix?

痴心易碎 提交于 2019-12-11 10:32:38

问题


I currently have a 1000 by 1000 matrix in R. There are 100 groups/communities in the matrix, and I wish to somehow verify if these 100 groups are appearing. For example, the first 10 columns/rows define a group, with the second 10 columns/rows defining another group, and so forth.

Is there a way in which I can visualize such a grouping is actually there? Ideally, I would have a heatmap that can show diagonal blocks of 10 by 10. I tried the heatmap function but it doesn't appear to do that.

Does anyone have any ideas here?


回答1:


I am not sure why you say that heatmap "doesn't appear to do that". It seems to work for me. I assume that your matrix is an adjacency matrix. I provide a very simple example where there are 100 highly connected groups of 10 nodes, but there are few connections between the groups.

Sample Data

## First generate the graph
set.seed(1234)
GX = erdos.renyi.game(10,0.8)
for(i in 1:99) {
    GX = GX + erdos.renyi.game(10,0.8) }

for(i in 0:99) {
    rv = sample(10,2)
    GX = add.edges(GX, c(i*10+rv[1], ((i+1)*10+rv[2]) %% 1000)) } 

## Now we need the adjacency matrix
AM = as.matrix(as_adjacency_matrix(GX))

I am going to plot this matrix two ways. First, just plot it.

heatmap(AM, Rowv=NA, Colv=NA, col=terrain.colors(16),
    labRow=FALSE, labCol=FALSE, revC=TRUE)

This is doing what it is supposed to do, But trying to put a 1000 x 1000 image onto a 2000 x 1000 computer screen can't come out very well. Although the groups are there down the diagonal, it is very difficult to see anything at this size. Instead, let's just view the upper left 100 x 100 part (first 10 groups) to increase the size.

heatmap(AM[1:100, 1:100], Rowv=NA, Colv=NA, col=terrain.colors(16),
    labRow=FALSE, labCol=FALSE, revC=TRUE)

It is easy to see the 10 highly connected groups of 10 and just a hint of connection between the groups.

To see anything with such a large matrix, you will need to magnify.



来源:https://stackoverflow.com/questions/50440120/in-r-is-there-a-similar-function-to-heatmap-to-visualize-groups-in-a-large-ma

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!