在igraph中生成社区图

9

我一直在寻找这个问题的答案,但是没有发现任何提及,所以我决定在这里发布。我正在尝试查看igraph或任何包是否提供了创建“社区图”的简单方法,其中每个节点表示网络中的一个社区,联系表示社区之间的联系。我能够让igraph中的社区检测算法正常工作,但是我找不到一种方法来折叠结果,以仅显示每个社区之间的联系。任何帮助将不胜感激。


1
可以使用igraph完成,但您并没有提供任何可重现的内容。这是我在igraph上做的一篇博客文章(链接1)(链接2)。是自我推广,但很合适 :) igraph的网站非常好,有许多示例。同样,我们可以通过一个示例数据集来进一步帮助您。 - Tyler Rinker
1个回答

21
你可以直接使用contract.vertices()函数。该函数将一组顶点合并为单个顶点,与你想要的方式基本相同。例如,
library(igraph)

## create example graph
g1 <- graph.full(5)
V(g1)$name <- 1:5    
g2 <- graph.full(5)
V(g2)$name <- 6:10
g3 <- graph.ring(5)
V(g3)$name <- 11:15
g <- g1 %du% g2 %du% g3 + edge('1', '6') + edge('1', '11')

## Community structure
fc <- fastgreedy.community(g)

## Create community graph, edge weights are the number of edges
cg <- contract.vertices(g, membership(fc))
E(cg)$weight <- 1
cg2 <- simplify(cg, remove.loops=FALSE)

## Plot the community graph
plot(cg2, edge.label=E(cg2)$weight, margin=.5, layout=layout.circle)

非常好的答案,谢谢!我在这里找到了contract.vertices的另一个用法示例 - Tapper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接