I have a list of lists containing the edgelists of plant-pollinator interactions, for a number bipartite networks. Using the igraph
r-package I am attempting to convert these lists into bipartite incidence matrices for analysis.
I am having trouble adding vertices (igraph::V
) to the igraph objects in the list graph_list
. Note: I would prefer to avoid for
loop solutions as my complete dataset is a list of 238 networks comprising >80k edges.
Reproducable example of my data.
network_list <- replicate(10, expr = {data.frame(plant = paste('plnt', '_', sample(x = letters, size = 10, replace = T), sep = ""),pollinator = paste('pollinator', '_', sample(x = letters, size = 10, replace = T), sep = ""))}, simplify = F)
library(tidyverse)
library(dplyr)
library(igraph)
I am trying to add the vertex sequence to each graph in the list. But the following code is not working as I am expecting.
graph_list <- lapply(network_list, graph_from_data_frame) #creates list of igraph objects from list of edgelists
list_mapping <- lapply(graph_list, bipartite.mapping) #map networks as bipartit
list_type<- lapply(list_mapping, with, type) #extract list of vertices for each network
graph_list <- mapply(c, graph_list, list_type) #ATTEMPT to add vertices to graph_list
incidence_list <- lapply(graph_list, get.incidence) #breaks
`Error in FUN(X[[i]], ...) : Not a graph object
get.incidence
is expecting bipartite graph
objects with types argument supplied.
For reference, I am following code which works for a single network.
example_network <- network_list[[1]] #select one network for example
net_graph <- graph_from_data_frame(example_network) #take the edge list and make it into a graph
bipartite.mapping(net_graph) #make bipartite graph
OUTPUT:
$res
[1] TRUE
$type
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
V(net_graph)$type <- bipartite.mapping(net_graph)$type #compute vertices ('type') and add as vector to graph
incidence_matrix <- get.incidence(net_graph) #produce incidence matrix
incidence_matrix[incidence_matrix > 1] <- 1 #force 0/1 edge, some rows are duplicated in random list resulting in values >1
where my desired format of each incidence matrix in the list is:
poll_z poll_g poll_h poll_d poll_r poll_i poll_l poll_x plnt_v 1 0 0 0 0 0 0 0 plnt_o 0 1 0 0 0 0 0 0 plnt_r 0 0 1 0 1 0 0 0 plnt_c 0 0 0 1 0 0 0 0 plnt_x 0 0 0 1 0 0 0 0 plnt_a 0 0 0 0 0 1 1 0 plnt_j 0 0 0 0 0 0 1 0 plnt_h 0 0 0 0 0 0 0 1
Also it would be a great help to have all possible duplicate values (i.e. values >1) in the list of matrices forced to equal 1.
You mapply
call using c
isn't correct. You could write your own little lambda function such as function(g, type) { V(g)$type <- type; return(g) }
directly inside mapply
, though there is also the igraph function vertex_attr<-
which can do the same thing directly.
I tend to use Map
rather than mapply
, because it is essentially the same function but never attempts to "simplify" the output the way mapply
does, which can lead to unexpected results. (Map(...)
is identical to mapply(..., SIMPLIFY = FALSE)
but requires fewer keystrokes).
At the end, to convert the list of matrices such that 0s remain 0s and all positive numbers become 1, we can do lapply(incidence_list, sign)
So your whole code might be something like this:
library(tidyverse)
library(igraph)
graph_list <- lapply(network_list, graph_from_data_frame)
list_mapping <- lapply(graph_list, bipartite.mapping)
list_type <- lapply(list_mapping, with, type)
graph_list <- Map(`vertex_attr<-`, graph_list, "type", value = list_type)
incidence_list <- lapply(graph_list, get.incidence)
incidence_list <- lapply(incidence_list, sign)
And your result would be:
incidence_list
#> [[1]]
#> pollinator_z pollinator_h pollinator_a pollinator_c pollinator_x
#> plnt_m 1 0 0 0 0
#> plnt_k 0 1 1 0 1
#> plnt_l 0 0 0 1 0
#> plnt_c 0 0 0 0 0
#> plnt_y 0 0 0 0 0
#> plnt_u 1 0 0 0 0
#> pollinator_b pollinator_n pollinator_l pollinator_i
#> plnt_m 0 0 1 1
#> plnt_k 0 0 0 0
#> plnt_l 0 0 0 0
#> plnt_c 1 0 0 0
#> plnt_y 0 1 0 0
#> plnt_u 0 0 0 0
#>
#> [[2]]
#> pollinator_j pollinator_t pollinator_o pollinator_s pollinator_l
#> plnt_e 1 0 0 0 0
#> plnt_t 0 1 0 0 0
#> plnt_j 0 0 1 0 0
#> plnt_v 0 0 0 1 0
#> plnt_o 0 0 1 0 1
#> plnt_p 0 0 0 0 0
#> plnt_b 0 0 0 0 0
#> plnt_u 0 0 0 0 0
#> pollinator_c pollinator_r pollinator_f
#> plnt_e 0 1 0
#> plnt_t 0 0 0
#> plnt_j 0 0 0
#> plnt_v 0 0 0
#> plnt_o 0 0 0
#> plnt_p 1 0 0
#> plnt_b 0 1 0
#> plnt_u 0 0 1
#>
#> [[3]]
#> pollinator_k pollinator_u pollinator_x pollinator_g pollinator_o
#> plnt_a 1 0 0 0 0
#> plnt_e 0 1 0 0 0
#> plnt_u 0 0 1 0 0
#> plnt_w 0 0 0 1 0
#> plnt_i 0 0 0 0 1
#> plnt_d 0 0 0 0 0
#> plnt_r 0 0 0 0 0
#> plnt_h 0 0 0 0 0
#> pollinator_s pollinator_e pollinator_h
#> plnt_a 0 0 1
#> plnt_e 0 0 0
#> plnt_u 0 0 0
#> plnt_w 0 0 0
#> plnt_i 0 0 0
#> plnt_d 1 0 0
#> plnt_r 0 1 0
#> plnt_h 1 0 0
#>
#> [[4]]
#> pollinator_r pollinator_i pollinator_l pollinator_m pollinator_z
#> plnt_n 1 0 0 0 0
#> plnt_d 0 1 0 0 0
#> plnt_g 0 0 1 0 0
#> plnt_v 0 0 0 1 0
#> plnt_y 0 0 0 0 1
#> plnt_l 0 0 0 0 0
#> plnt_r 0 0 0 0 0
#> plnt_j 0 0 0 0 0
#> pollinator_k pollinator_o pollinator_c pollinator_j pollinator_x
#> plnt_n 0 0 0 0 0
#> plnt_d 0 0 0 0 0
#> plnt_g 0 0 0 0 0
#> plnt_v 0 0 1 0 0
#> plnt_y 0 0 0 0 0
#> plnt_l 1 0 0 0 1
#> plnt_r 0 1 0 0 0
#> plnt_j 0 0 0 1 0
#>
#> [[5]]
#> pollinator_l pollinator_p pollinator_f pollinator_x pollinator_m
#> plnt_m 1 0 0 1 0
#> plnt_i 0 1 0 0 0
#> plnt_x 0 0 1 0 0
#> plnt_k 0 0 0 0 1
#> plnt_l 0 0 0 0 0
#> plnt_z 0 0 0 0 0
#> pollinator_u pollinator_e pollinator_r pollinator_b pollinator_a
#> plnt_m 0 0 0 0 0
#> plnt_i 0 0 1 0 1
#> plnt_x 1 0 0 0 0
#> plnt_k 0 0 0 0 0
#> plnt_l 0 1 0 0 0
#> plnt_z 0 0 0 1 0
#>
#> [[6]]
#> pollinator_d pollinator_x pollinator_n pollinator_y pollinator_s
#> plnt_k 1 0 0 0 0
#> plnt_b 0 1 0 0 0
#> plnt_m 0 0 1 0 0
#> plnt_g 0 0 0 1 0
#> plnt_z 0 0 0 0 1
#> plnt_j 0 0 0 0 0
#> plnt_x 0 0 0 0 0
#> plnt_v 0 0 0 0 0
#> pollinator_k pollinator_i pollinator_c
#> plnt_k 0 0 0
#> plnt_b 0 0 0
#> plnt_m 1 0 0
#> plnt_g 0 0 0
#> plnt_z 0 0 0
#> plnt_j 1 0 0
#> plnt_x 0 1 0
#> plnt_v 0 0 1
#>
#> [[7]]
#> pollinator_r pollinator_x pollinator_k pollinator_j pollinator_u
#> plnt_q 1 0 0 0 0
#> plnt_o 0 1 0 0 0
#> plnt_z 0 0 1 0 0
#> plnt_t 0 1 0 0 0
#> plnt_g 0 0 0 1 0
#> plnt_e 0 0 0 0 1
#> plnt_p 0 0 0 0 1
#> plnt_r 0 0 0 0 0
#> plnt_n 0 0 0 0 0
#> pollinator_a pollinator_s pollinator_d
#> plnt_q 0 0 0
#> plnt_o 0 0 1
#> plnt_z 0 0 0
#> plnt_t 0 0 0
#> plnt_g 0 0 0
#> plnt_e 0 0 0
#> plnt_p 0 0 0
#> plnt_r 1 0 0
#> plnt_n 0 1 0
#>
#> [[8]]
#> pollinator_g pollinator_b pollinator_m pollinator_v pollinator_c
#> plnt_w 1 0 0 1 0
#> plnt_l 0 1 0 0 0
#> plnt_s 0 0 1 0 0
#> plnt_c 0 0 0 1 0
#> plnt_p 0 0 0 0 1
#> plnt_x 0 0 0 0 0
#> plnt_d 0 0 0 0 0
#> plnt_r 0 0 0 0 0
#> pollinator_o pollinator_z pollinator_t pollinator_j
#> plnt_w 0 0 0 0
#> plnt_l 0 0 0 0
#> plnt_s 0 0 0 0
#> plnt_c 0 0 0 1
#> plnt_p 0 0 0 0
#> plnt_x 1 0 0 0
#> plnt_d 0 1 0 0
#> plnt_r 0 0 1 0
#>
#> [[9]]
#> pollinator_g pollinator_x pollinator_f pollinator_v pollinator_q
#> plnt_t 1 0 0 0 0
#> plnt_k 0 1 0 0 0
#> plnt_l 0 0 1 0 0
#> plnt_r 0 0 0 1 0
#> plnt_b 0 0 0 0 1
#> plnt_s 0 0 0 0 0
#> plnt_m 0 0 0 0 0
#> plnt_n 0 0 1 0 0
#> plnt_g 0 0 0 0 0
#> pollinator_z pollinator_e pollinator_n pollinator_w
#> plnt_t 0 0 0 0
#> plnt_k 0 0 0 0
#> plnt_l 1 0 0 0
#> plnt_r 0 0 0 0
#> plnt_b 0 0 0 0
#> plnt_s 0 1 0 0
#> plnt_m 0 0 1 0
#> plnt_n 0 0 0 0
#> plnt_g 0 0 0 1
#>
#> [[10]]
#> pollinator_i pollinator_q pollinator_g pollinator_b pollinator_p
#> plnt_p 1 0 0 0 0
#> plnt_h 0 1 0 0 0
#> plnt_y 0 0 1 0 0
#> plnt_s 0 0 0 1 0
#> plnt_z 0 0 0 0 1
#> plnt_j 0 0 0 0 0
#> plnt_e 0 0 0 0 0
#> plnt_m 0 0 0 0 0
#> plnt_x 0 0 0 0 0
#> pollinator_o pollinator_z pollinator_a pollinator_y pollinator_h
#> plnt_p 0 0 0 0 0
#> plnt_h 0 0 0 0 0
#> plnt_y 0 0 0 0 0
#> plnt_s 0 0 0 0 0
#> plnt_z 0 0 0 0 0
#> plnt_j 1 1 0 0 0
#> plnt_e 0 0 1 0 0
#> plnt_m 0 0 0 1 0
#> plnt_x 0 0 0 0 1
Created on 2023-02-19 with reprex v2.0.2