如何使用不同的输入在 igraph for R 中绘制社区多边形?

How to use a different input to draw community polygons in igraph for R?

你能帮帮我吗?

我喜欢用 igraph 为 R 绘制网络。一个不错的功能是在给定算法检测到的社区 周围绘制 多边形。

当您使用 igraph 中内置的社区检测算法之一时,这非常简单。就像这个带有随机二分图的例子:

library(igraph)
graph <- sample_bipartite(10, 10, p = 0.5)
graph
graph.lou = cluster_louvain(graph)
graph.lou$membership
length(graph.lou$membership)
plot(graph.lou, graph)

但是我怎样才能使用另一种输入来绘制这些多边形呢?

例如,我通常使用 R 的包 bipartite 来计算模块化,因为它有其他更适合双模网络的算法。

所以我尝试使用 bipartite 的输出作为在 igraph 中绘制社区多边形的输入。如以下示例所示:

library(bipartite)
matrix <- as_incidence_matrix(graph)
matrix
matrix.bec = computeModules(matrix, method = "Beckett")
modules <- module2constraints(matrix.bec)
modules
length(modules)
plot(modules, graph)

computeModules 函数的输出中,我可以使用 module2constraints 函数提取具有社区成员资格的向量。当我尝试将其用作绘图输入时,我收到此错误消息:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ

是否可以在 igraph 中使用 bipartite 的输出,以便在社区周围自动绘制多边形?

我查看了文档,在 Whosebug 上进行了搜索,尝试了一些技巧,但没有找到解决方案。

非常感谢!

在另一个 !

的帮助下,我找到了解决方案

实际上,另一种在 igraph 中为 R 的社区绘制多边形的方法是使用函数 plot.

的参数 mark.groups

但是,此参数仅接受社区成员列表。因此,如果您想以 vector 的格式与 igraph 对象一起使用包 bipartite 的输出,则需要将其转换为首先列出

原题描述的vectormodules中包含的信息需要补充顶点名称,首先成为data frame,然后是 list:

number <- seq(1:10)
row <- "row"
rowlabels <- paste(row, number, sep = "")
column <- "col"
columnlabels <- paste(column, number, sep = "")

matrix <- matrix(data = rbinom(100,size=1,prob=0.5), nrow = 10, ncol = 10,
                  dimnames = list(rowlabels, columnlabels))

library(bipartite)
matrix.bec <- computeModules(matrix, method = "Beckett")
modules <- module2constraints(matrix.bec)

df <- data.frame(c(rownames(matrix), colnames(matrix)), modules) 
colnames(df) <- c("vertices", "modules")
list <- split(df$vertices, df$modules)

现在对象 list 可以与 igraph 对象一起用作绘图输入:

library(igraph)
graph <- graph_from_incidence_matrix(matrix, directed = F)

plot(graph,
     mark.groups = list)

这是让 bipartiteigraph 相互交谈的一种方式!

非常感谢!