Say that we have such a matrix:
The 1 indicates a direction from the row to the column, a 2 means a bi-direction, a zero no-relationship (diagonal is by default always 0). I would like then to build a network that represents these relationships, so, for example, one-side arrow from D to A, two-side arrow between A and B, and so on. How to do this using the library igraph
?
You can turn your (custom?) graph representation to adjacency matrix by replacing twos with ones, from there it's just igraph::graph_from_adjacency_matrix()
library(igraph)
m <- matrix(c(0,2,1,0,
2,0,0,0,
0,1,0,1,
1,0,0,0),
nrow = 4, byrow = TRUE,
dimnames = list(LETTERS[1:4],LETTERS[1:4]))
m
#> A B C D
#> A 0 2 1 0
#> B 2 0 0 0
#> C 0 1 0 1
#> D 1 0 0 0
# relpace 2s with 1s and you'll have adjacency matrix
m[m == 2] <- 1
m
#> A B C D
#> A 0 1 1 0
#> B 1 0 0 0
#> C 0 1 0 1
#> D 1 0 0 0
plot(graph_from_adjacency_matrix(m))
Created on 2024-06-02 with reprex v2.1.0