Reputation: 883
I have two lists of around 80 dataframes- one for nodes and another for edges. I would like to use the igraph function graph_from_dataframe to join the nodes and edges dataframes make a single list of igraph objects. At the moment I have been trying this:
glst <- mapply(graph_from_data_frame,
d=ListOfEdgeDfs,
vertices=ListOfNodeDfs,
SIMPLIFY = FALSE,
directed =F)
However, this gives the error message:
Error in (function (d, directed = TRUE, vertices = NULL) :
Some vertex names in edge list are not listed in vertex data frame
Does anyone have any ideas as to how I could do this?
Upvotes: 0
Views: 725
Reputation: 23109
The following works (for list of 2 graphs, the same should work for any number of graphs, can you make sure that the data frame structures are similar):
ListOfEdgeDfs <- list(data.frame(from=letters[1:3], to=c('b', 'c', 'd')),
data.frame(from=rep('a',3), to=c('b', 'c', 'd')))
ListOfEdgeDfs
#[[1]]
# from to
#1 a b
#2 b c
#3 c d
#[[2]]
# from to
#1 a b
#2 a c
#3 a d
ListOfNodeDfs <- list(data.frame(node=letters[1:4]), data.frame(node=letters[1:4]))
ListOfNodeDfs
#[[1]]
# node
#1 a
#2 b
#3 c
#4 d
#[[2]]
# node
#1 a
#2 b
#3 c
#4 d
library(igraph)
glst <- mapply(graph_from_data_frame,
d=ListOfEdgeDfs,
vertices=ListOfNodeDfs,
SIMPLIFY = FALSE,
directed =F)
par(mfrow=c(1,2))
#plot(glst[[1]])
#plot(glst[[2]])
[EDITED]
Try this:
n <- 2 # number of graphs
par(mfrow=c(2,1)) # if you have 100 graphs, try par(mfrow=c(10,10)) e.g.
invisible(sapply(1:n, function(i) plot(glst[[i]], vertex.size=50)))
Upvotes: 2