Igraph - способ извлечь, какие узлы и в какие сообщества - PullRequest
1 голос
/ 09 мая 2019

Я нашел этот код онлайн здесь: 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?

Этовызывая мою проблему, или я могу найти ответ где-нибудь еще?

...