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

dee*_*nes 14 r igraph

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

Ant*_*ine 6

为了扩展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)
}
Run Code Online (Sandbox Code Playgroud)

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

E(g)$weight=apply(get.edgelist(g),1,weight.community,membership,10,1)
Run Code Online (Sandbox Code Playgroud)

然后,简单地使用接受边权重的布局算法,例如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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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

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


Alb*_*mes 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) 
}
Run Code Online (Sandbox Code Playgroud)

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

然后将权重转移到weight节点中的插槽:

E(graph)$weight <- edge.weights(community, graph)
Run Code Online (Sandbox Code Playgroud)

最后使用一个使用权重的布局算法layout_with_fr(fruchterman.reingoldin 的新名称igraph 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" )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我通过功能的多级优化模块化来检测社区cluster_louvain:

Communitykarate <- cluster_louvain(karate)
Run Code Online (Sandbox Code Playgroud)

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

prettyColors <- c("turquoise4", "azure4", "olivedrab","deeppink4")
communityColors <- prettyColors[membership(Communitykarate)]
Run Code Online (Sandbox Code Playgroud)

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

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])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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)")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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)")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述