如何在igraph中制作分组布局?

时间:2013-05-05 23:34:24

标签: r igraph

igraph中,在应用模块化算法查找图形社区之后,我想绘制一个网络布局,清楚地显示不同的社区及其连接。类似于Cytoscape中的“组属性布局”:我希望显示每个组/社区的成员彼此接近,并在组/社区之间保持一定距离。我在igraph中找不到任何提供此功能的功能。在发布这个问题时,我已经找到了一个简单的d.i.y解决方案,我将把它作为答案发布。但我想知道是否有更好的可能性,或更详细的解决方案?

4 个答案:

答案 0 :(得分:5)

为了扩展Gabor的建议,我创建了这个功能:

weight.community=function(row,membership,weigth.within,weight.between){
if(as.numeric(membership[which(names(membership)==row[1])])==as.numeric(membership[which(names(membership)==row[2])])){
weight=weigth.within
}else{
weight=weight.between
}
return(weight)
}

只需将其应用于图表边缘矩阵的行(由get.edgelist(your_graph))给出,以设置新的边缘权重(成员资格是任何社区检测算法结果的成员资格向量):

E(g)$weight=apply(get.edgelist(g),1,weight.community,membership,10,1)

然后,只需使用接受边权重的布局算法,例如Gabor建议的fruchterman.reingold。您可以调整权重参数以获取所需的图形。例如:

E(g)$weight=apply(get.edgelist(g),1,weight.community,membership,10,1)
g$layout=layout.fruchterman.reingold(g,weights=E(g)$weight)
plot(g)

enter image description here

E(g)$weight=apply(get.edgelist(g),1,weight.community,membership,1000,1)
g$layout=layout.fruchterman.reingold(g,weights=E(g)$weight)
plot(g)

enter image description here

注1:边缘的透明度/颜色是我的图形的其他参数。我有社区的彩色节点,表明它确实有效。

注意2:确保使用membership(comm)而非comm$membership,其中comm是社区检测算法的结果(例如comm=leading.eigenvector.community(g))。原因是在第一种情况下,你得到一个带有名字(我们想要的)的数字向量,在第二种情况下,得到一个没有名字的相同向量。

要获得多个社区检测算法的一致意见,请参阅此function

答案 1 :(得分:5)

受到Antoine建议的启发,我创建了这个功能:

edge.weights <- function(community, network, weight.within = 100, weight.between = 1) {
bridges <- crossing(communities = community, graph = network)
weights <- ifelse(test = bridges, yes = weight.between, no = weight.within)
return(weights) 
}

该功能也是如此;只需将社区对象放在社区插槽中,将图形放在网络插槽中即可。我会离开weight.between = 1并调整weight.within值。

然后将权重转移到节点中的weight位置:

E(graph)$weight <- edge.weights(community, graph)

最后使用布局算法,使用layout_with_fr等权重(fruchterman.reingoldigraph 1.0.1的新名称)。

我以Zachary的空手道俱乐部网络为例。

library(igraph)
library(igraphdata)
#I load the network
data(karate)
#for reproducible purposes
set.seed(23548723)
karateLayout <- layout_with_fr(karate)
par(mar = c(0,0,2,0))
plot(karate, vertex.size = 10, vertex.color = "steelblue4", edge.width = 1, 
vertex.label = NA, edge.color = "darkgrey", layout = karateLayout,
main = "Zachary's karate club network" )

enter image description here

我使用cluster_louvain函数通过多级优化模块化来检测社区:

Communitykarate <- cluster_louvain(karate)

接下来是个人偏好超过默认值:

prettyColors <- c("turquoise4", "azure4", "olivedrab","deeppink4")
communityColors <- prettyColors[membership(Communitykarate)]

使用颜色突出显示社区的图表是:

plot(x = Communitykarate, y = karate, edge.width = 1, vertex.size = 10, 
vertex.label = NA, mark.groups = NULL, layout = karateLayout, col = communityColors,
main = "Communities in Zachary's karate club network",
edge.color = c("darkgrey","tomato2")crossing(Communitykarate, karate) + 1])

enter image description here

现在,这个问题存在的意义。

E(karate)$weight <- edge.weights(Communitykarate, karate)
# I use the original layout as a base for the new one
karateLayoutA <- layout_with_fr(karate, karateLayout)
# the graph with the nodes grouped
plot(x = Communitykarate, y = karate, edge.width = 1, vertex.size = 10, 
mark.groups = NULL, layout = karateLayoutA, vertex.label = NA, col = communityColors, 
c("darkgrey","tomato2")[crossing(Communitykarate, karate) + 1],
main = "Communities in Zachary's karate club network (grouped)")

enter image description here

如果你尝试更多的体重,你将拥有:

E(karate)$weight <- edge.weights(Communitykarate, karate, weight.within = 1000)
karateLayoutB <- layout_with_fr(karate, karateLayout)
plot(x = Communitykarate, y = karate, edge.width = 1, vertex.size = 10,
 mark.groups = NULL, layout = karateLayoutB, vertex.label = NA, col = communityColors, 
c("darkgrey","tomato2")[crossing(Communitykarate, karate) + 1],
main = "Communities in Zachary's karate club network (grouped)")

enter image description here

答案 2 :(得分:3)

函数layout.modular根据任何igraph社区检测方法的结果为图形提供分组布局:

c <- fastgreedy.community(G)

layout.modular <- function(G,c){
nm <- length(levels(as.factor(c$membership)))
gr <- 2
while(gr^2<nm){
    gr <- gr+1
}
i <- j <- 0
for(cc in levels(as.factor(c$membership))){
    F <- delete.vertices(G,c$membership!=cc)
    F$layout <- layout.kamada.kawai(F)
    F$layout <- layout.norm(F$layout, i,i+0.5,j,j+0.5)
    G$layout[c$membership==cc,] <- F$layout
    if(i==gr){
        i <- 0
        if(j==gr){
            j <- 0
        }else{
            j <- j+1
        }
    }else{
        i <- i+1
    }
}
return(G$layout)
}

G$layout <- layout.modular(G,c)
V(G)$color <- rainbow(length(levels(as.factor(c$membership))))[c$membership]
plot(G)

答案 3 :(得分:3)

一种解决方案是基于模块化设置图的边权重。将模块内边缘设置为较大的重量,并将模块之间的边缘设置为较小的重量。然后调用layout.fruchterman.reingold()或任何支持边权重的算法。

您可能需要使用实际重量值,因为这取决于您的图表。