Ok, so I think what has happened here, is the number of metabolites you have, just happens to be the same as the number of taxa you have. Otherwise graph.adjacency
would throw an error: It wants a square matrix of adjacencies, the rows AND the columns of cor.matrix$r
should contain all the nodes in the graph.
Your graph is bipartite: Metabolites can be connected to taxa, and taxa can be connected to metabolites, but metabolites are never connected to other metabolites, and taxa are never connected to other taxa. When you run corr.test
, your rows are your taxa, and your columns are your metabolites.
I am not familiar with the igraph
package: there might be a more suitable method than graph.adjacency
for handling bipartite graphs. In lieu of that, we can force our adjacency matrix, cor.matrix
, to contain all nodes:
Run this immediately after you obtain cor.matrix
:
nNodes <- ncol(cor.matrix$r) + nrow(cor.matrix$r)
nodeNames <- c(colnames(cor.matrix$r), rownames(cor.matrix$r))
adj <- matrix(0, nNodes, nNodes, dimnames=list(nodeNames, nodeNames))
adj[(ncol(cor.matrix$r)+1):nrow(adj), 1:ncol(cor.matrix$r)] <- cor.matrix$r
adj[1:ncol(cor.matrix$r), (ncol(cor.matrix$r)+1):ncol(adj)] <- t(cor.matrix$r)
Now you can obtain graph.f
from adj
, and run the rest of your code:
graph.f <- graph.adjacency(adj, weighted=TRUE, mode="upper")