2017-04-08 55 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)