2017-04-08 27 views
1

简化图我有一个网络,它看起来像这样提取的igraph

library(igraph) 
library(igraphdata) 

data("kite") 
plot(kite) 

The graph I am working with

我运行一个社区检测,结果看起来像这样

community <- cluster_fast_greedy(kite) 
plot(community,kite) 

The result of the community detection mapped on the network

现在我想提取一个基于网络社区。边权重应该是社区之间的联系数量(社区相互连接的强度),顶点属性应该是社区中的节点数(称为numnodes)。

d <- data.frame(E=c(1, 2, 3), 
       A=c(2, 3, 1)) 
g2 <- graph_from_data_frame(d, directed = F) 
E(g2)$weight <- c(5, 1, 1) 
V(g2)$numnodes <- c(4,3,3) 

plot.igraph(g2,vertex.label=V(g2)$name, edge.color="black",edge.width=E(g2)$weight,vertex.size=V(g2)$numnodes) 

的曲线应该是这样的 Final product 一个节点比其他大,一个边缘具有很多的重量相比于其他。

回答

1

据我所知,igraph没有计算连接顶点组的边的方法。因此,要计算连接社区的边缘,您需要遍历每对社区。要计算每个社区的成员数量,可以使用sizes方法。

library(igraph) 
library(igraphdata) 

data("kite") 
plot(kite) 

community <- cluster_fast_greedy(kite) 
plot(community,kite) 

cedges <- NULL 

for(i in seq(1,max(community$membership) - 1)){ 
    for(j in seq(i + 1, max(community$membership))){ 
     imembers <- which(community$membership == i) 
     jmembers <- which(community$membership == j) 
     weight <- sum(
      mapply(function(v1) mapply(
       function(v2) are.connected(kite, v1, v2), 
       jmembers), 
      imembers) 
     ) 
     cedges <- rbind(cedges, c(i, j, weight)) 
    } 
} 

cedges <- as.data.frame(cedges) 
names(cedges)[3] <- 'weight' 
cgraph <- graph_from_data_frame(cedges, directed = FALSE) 

V(cgraph)$numnodes <- sizes(community) 

plot.igraph(cgraph, 
    vertex.label = V(cgraph)$name, 
    edge.color = "black", 
    edge.width = E(cgraph)$weight, 
    vertex.size = V(cgraph)$numnodes)