gpt4 book ai didi

r - 在igraph中按簇折叠图

转载 作者:行者123 更新时间:2023-12-04 06:57:22 24 4
gpt4 key购买 nike

我想将图折叠到其各自的社区/集群中。让我用下面的玩具示例来说明这一点:

set.seed(123)

#toy graph
g <- barabasi.game(10) %>%
as.undirected()

#identify communities
c_g <- fastgreedy.community(g)

如下图所示,共有三个社区。

enter image description here

我想减少顶点的折叠,以使结果图中的顶点对应于先前顶点的隶属关系。参见图表。

enter image description here

我是 igraph包的新手,我不熟悉处理 igraph对象的最佳方法。

最佳答案

您可以尝试contract:

library(igraph)
set.seed(123)
g <- barabasi.game(10) %>% as.undirected()
c_g <- fastgreedy.community(g)
V(g)$name <- letters[1:vcount(g)]

g2 <- contract(g, membership(c_g), vertex.attr.comb=toString)

par(mfrow=c(1,2))
plot(g, vertex.color=membership(c_g))
plot(simplify(g2), vertex.color=1:vcount(g2))

enter image description here

关于r - 在igraph中按簇折叠图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35000554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com