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

Could you please help me?

I love plotting networks with igraph. One nice feature is drawing polygons around the communities detected by a given algorithm.

When you use one of the community detection algorithms built in igraph, that’s pretty straightforward. Like in this example with a random bipartite graph:

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)

But how can I use another kind of input to draw those polygons?

For instance, I usually calculate modularity using the package bipartite, because it has other algorithms that are better suited for two-mode networks.

So I’m trying to use the output from bipartite as an input for drawing community polygons in igraph. As in the following example:

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

From the output of the computeModules function I’m able to extract a vector with community memberships using the module2constraints function. When I try to use it as a plotting input, I get this error message:

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

Is it possible to use this output from bipartite in igraph, so polygons are automatically drawn around the communities?

I’ve looked into the documentation, searched here in the forum and on StackOverflow, experimented some tricks, but found no solution.

Thank you very much!

The info that is stored in modules is not enough for the plot function. You need an object with the information which node belongs to which group. I wrote a quick’n’dirty solution, and I hope someone might come up with a function or a better solution:

graph.modules <- NULL
graph.modules$`1` <- which(modules=="1")
graph.modules$`2` <- which(modules=="2")
graph.modules$`3` <- which(modules=="3")
graph.modules$`4` <- which(modules=="4")
graph.modules

plot(graph, mark.groups=graph.modules)