问题描述
我在上一个问题中在R igraph中使用组创建变量,我想在我的数据类型中将具有相同组/颜色的顶点放在一起,就像这样 https://lists.nongnu.org/archive/html/igraph-help/2012-03/pngFA9V_3yRcA.png .预先感谢
from my previous question here Creating Variables with Group in R igraph, 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. Thanks in advance
推荐答案
我已经在我的软件包中实现了这样的功能 NetPathMiner ,称为 layoutVertexByAttribute
.
I have implemented such a function in my package 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)
您可以在此处或查看源代码查看 vignette 了解更多详细信息
You can look at the source code here or view the vignette for more details.
由于如果您不使用Bioconductor,安装软件包似乎有些困难,因此我将在此处编写该功能的简单版本.
Since installing the package seems a bit difficult if you don't use Bioconductor, I will write a simpler version of the function here.
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)
}
要在您的示例中使用它:
To use it with your example:
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)
这篇关于基于社区的igraph组顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!