揭示 igraph 中的交互集群

标签 r cluster-analysis igraph hierarchical-clustering

我有一个交互网络,我使用以下代码制作邻接矩阵,随后计算网络节点之间的相异性,然后将它们聚类以形成模块:

ADJ1=abs(adjacent-mat)^6
dissADJ1<-1-ADJ1
hierADJ<-hclust(as.dist(dissADJ1), method = "average")

现在我希望在绘制 igraph 时显示这些模块。

g<-simplify(graph_from_adjacency_matrix(adjacent-mat, weighted=T))
plot.igraph(g)

然而,到目前为止,我发现将 hclust 输出转换为图形的唯一方法是按照以下教程:http://gastonsanchez.com/resources/2014/07/05/Pretty-tree-graph/

phylo_tree = as.phylo(hierADJ)
graph_edges = phylo_tree$edge
graph_net = graph.edgelist(graph_edges)
plot(graph_net)

enter image description here

这对于分层沿袭很有用,但我只想要与集群密切交互的节点,如下所示:

enter image description here

谁能推荐如何使用命令(例如来自 igraph 的组件)来显示这些集群?

最佳答案

igraph 提供了一堆不同的 layout algorithms用于在图中放置节点。

对于像这样的加权网络,一个很好的起点是 force-directed layout (由 igraph 中的 layout.fruchterman.reingold 实现)。

下面是一个使用力导向布局的例子,使用了一些简单的模拟数据。

首先,我们创建一些模拟数据和集群,以及一些“噪音”以使其更真实:

library('dplyr')
library('igraph')
library('RColorBrewer')

set.seed(1)

# generate a couple clusters
nodes_per_cluster <- 30
n <- 10

nvals <- nodes_per_cluster * n

# cluster 1 (increasing) 
cluster1 <- matrix(rep((1:n)/4, nodes_per_cluster) + 
                   rnorm(nvals, sd=1),
                   nrow=nodes_per_cluster, byrow=TRUE)

# cluster 2 (decreasing)
cluster2 <- matrix(rep((n:1)/4, nodes_per_cluster) + 
                   rnorm(nvals, sd=1),
                   nrow=nodes_per_cluster, byrow=TRUE)

# noise cluster
noise <- matrix(sample(1:2, nvals, replace=TRUE) +
                rnorm(nvals, sd=1.5),
                nrow=nodes_per_cluster, byrow=TRUE)

dat <- rbind(cluster1, cluster2, noise)
colnames(dat) <- paste0('n', 1:n)
rownames(dat) <- c(paste0('cluster1_', 1:nodes_per_cluster), 
                   paste0('cluster2_', 1:nodes_per_cluster),
                   paste0('noise_',    1:nodes_per_cluster))

接下来,我们可以使用Pearson correlation构建我们的邻接矩阵:

# create correlation matrix
cor_mat <- cor(t(dat))

# shift to [0,1] to separate positive and negative correlations
adj_mat <- (cor_mat + 1) / 2

# get rid of low correlations and self-loops
adj_mat <- adj_mat^3
adj_mat[adj_mat < 0.5] <- 0
diag(adj_mat) <- 0

使用 hclustcutree 对数据进行聚类:

# convert to dissimilarity matrix and cluster using hclust
dissim_mat <- 1 - adj_mat

dend <- dissim_mat %>% 
    as.dist %>% 
    hclust

clusters = cutree(dend, h=0.65)

# color the nodes
pal = colorRampPalette(brewer.pal(11,"Spectral"))(length(unique(clusters)))
node_colors <- pal[clusters]

最后,从邻接矩阵创建一个 igraph 图并使用 fruchterman.reingold 布局绘制它:

# create graph
g <- graph.adjacency(adj_mat, mode='undirected', weighted=TRUE)

# set node color and plot using a force-directed layout (fruchterman-reingold)
V(g)$color <- node_colors
coords_fr = layout.fruchterman.reingold(g, weights=E(g)$weight)

# igraph plot options
igraph.options(vertex.size=8, edge.width=0.75) 

# plot network
plot(g, layout=coords_fr, vertex.color=V(g)$color)

在上面的代码中,我生成了两个相关行的“集群”,以及第三组“噪声”。

层次聚类(hclust + cuttree)用于将数据点分配给聚类,它们根据聚类成员身份进行着色。

结果是这样的:

enter image description here

有关使用 igraph 聚类和绘制图形的更多示例,请查看:http://michael.hahsler.net/SMU/LearnROnYourOwn/code/igraph.html

关于揭示 igraph 中的交互集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162607/

相关文章:

r - R 中的屏蔽函数列表

css - 如何在 xaringan 演示文稿中隐藏代码块?

r - GGally 中的错误 - 确保您的 'columns' 值小于 5

r - clusplot - 显示变量

c - 边权重关联

python - 特征向量中心性的快速计算在 networkx 中花费的时间太长

r - 个体数字(核苷酸)中的分割区间(基因组区域)

opencv - 训练数据簇的 BOW 预测

r - 使用 R 中的 Statnet 进行加权网络中的中心性测量

R - 高性能 Amazon EC2 比 i7 慢?