基于社区的 igraph 组顶点

igraph group vertices based on community

来自我之前的问题 , I want to put the vertices with the same group/color close together in my type of data just like this one https://lists.nongnu.org/archive/html/igraph-help/2012-03/pngFA9V_3yRcA.png。提前致谢

我已经在我的包中实现了这样的功能NetPathMiner, called layoutVertexByAttribute

library(igraph)
library(NetPathMiner)

g <- graph.data.frame(message)
g <- setAttribute(g, "sender", sender_country)

l = layoutVertexByAttr(g, "sender", cluster.strength=10, layout=layout.kamada.kawai)

plotNetwork(g, vertex.color="sender",layout=l)

您可以查看源代码here or view the vignette了解更多详情。

编辑:

如果不使用Bioconductor,安装包似乎有点困难,我会在这里写一个更简单的函数版本。

layout.by.attr <- function(graph, wc, cluster.strength=1,layout=layout.auto) {  
        g <- graph.edgelist(get.edgelist(graph)) # create a lightweight copy of graph w/o the attributes.
        E(g)$weight <- 1

        attr <- cbind(id=1:vcount(g), val=wc)
        g <- g + vertices(unique(attr[,2])) + igraph::edges(unlist(t(attr)), weight=cluster.strength)

        l <- layout(g, weights=E(g)$weight)[1:vcount(graph),]
        return(l)
}

要在您的示例中使用它:

g <- graph.data.frame(message) 

l = layoutVertexByAttr(g, sender_country, cluster.strength=10, layout=layout.kamada.kawai)

plot.igraph(g, vertex.color=sender_country, layout=l)