我在这里https://blog.revolutionanalytics.com/2015/08/contracting-and-simplifying-a-network-graph.html
在线找到了此代码library(igraph)
# Download prepared igraph file from github
gs <- readRDS("pdb/depGraph-CRAN.rds")
set.seed(42)
# Compute communities (clusters)
cl <- walktrap.community(gs, steps = 5)
cl$degree <- (degree(gs)[cl$names])
# Assign node with highest degree as name for each cluster
cl$cluster <- unname(ave(cl$degree, cl$membership,
FUN=function(x)names(x)[which.max(x)])
)
V(gs)$name <- cl$cluster
# Contract graph ----------------------------------------------------------
# Contract vertices
E(gs)$weight <- 1
V(gs)$weight <- 1
gcon <- contract.vertices(gs, cl$membership,
vertex.attr.comb = list(weight = "sum", name = function(x)x[1], "ignore"))
# Simplify edges
gcon <- simplify(gcon, edge.attr.comb = list(weight = "sum", function(x)length(x)))
gcc <- induced.subgraph(gcon, V(gcon)$weight > 20)
V(gcc)$degree <- unname(degree(gcc))
# ------------------------------------------------------------------------
set.seed(42)
par(mar = rep(0.1, 4))
g.layout <- layout.kamada.kawai(gcc)
plot.igraph(gcc, edge.arrow.size = 0.1, layout = g.layout, vertex.size = 0.5 * (V(gcc)$degree))
此代码收缩节点并简化边缘。它将我的图形从500多个节点减少到39个左右,这真是太好了!但是,我想知道哪个节点最终位于哪个群集中,以便检查该过程是否有意义。
使用代码时也会出现此错误:
> V(gs)$name <- cl$cluster
Warning message:
In length(vattrs[[name]]) <- vc : length of NULL cannot be changed
> (degree(gs)[cl$names])
numeric(0) <-- there seems to be nothing?
> unname(ave(cl$degree, cl$membership,
+ FUN=function(x)names(x)[which.max(x)]))
numeric(0) <-- there seems to be nothing?
这是引起我的问题还是在其他地方找到答案?