building a graph using two sets of nodes in R

102 views Asked by At

I am trying to build a graph in R using two sets of nodes with the same size. each list has some numbers in it representing node numbers. I want each node in the first list be connected to the corresponding node in the other list. For example list1[1] should be connected to list2[1] and so on.

1

There are 1 answers

0
josliber On BEST ANSWER

One way to accomplish this would be to construct your graph with an edge list passed to the graph.data.frame function. The first column will be all the nodes in your first vector and the second will be all the nodes in your second vector:

list1 <- c("A", "B", "C")
list2 <- c("D", "E", "F")
library(igraph)
g <- graph.data.frame(cbind(list1, list2))
plot(g)

enter image description here