Could you please help me?
I work with ecological interactions, and some of them are modeled as multilayer networks (multiplex, to be precise).
The best way to input this kind of data into R's package igraph is by using edge and node lists. Unfortunately, my collaborators never have their data organized like that, but rather as incidence matrices (bipartite networks).
I always ask them to at least have those matrices organized precisely with the same dimensions and the same order of row and column labels, so they can be easily combined.
Having those matrices at hand, I then run a long code based on the following steps:
Read two or more incidence matrices into R;
Extract their edge and vertex lists;
Add information on edge type to each edge list;
Add information on vertex class to each vertex list;
Merge those edge lists and vertex lists separately;
Read those merged lists into igraph to create a multilayer graph.
I'm looking for a simpler solution. I've tried using the function union, but it merges the graphs and their edges without keeping information on edge types. See what happens in this example with random matrices:
number <- seq(1:10)
row <- "row"
rowlabels <- paste(row, number, sep = "")
column <- "col"
columnlabels <- paste(column, number, sep = "")
matrix1 <- matrix(data = rbinom(100,size=1,prob=0.5), nrow = 10, ncol = 10,
dimnames = list(rowlabels, columnlabels))
matrix2 <- matrix(data = rbinom(100,size=1,prob=0.5), nrow = 10, ncol = 10,
dimnames = list(rowlabels, columnlabels))
graph1 <- graph_from_incidence_matrix(matrix1, directed = F)
graph2 <- graph_from_incidence_matrix(matrix2, directed = F)
E(graph1)$type = "layer1"
E(graph2)$type = "layer2"
graph_multi <- union(graph1, graph2)
graph_multi
E(graph_multi)$type
Is there an easier way to combine two or more incidence matrices to make a multilayer graph in igraph?
Thank you very much!
I would convert the data to data frames, combine to a single edgelist and make the graph in the final step. Something like the following: