Reputation: 317
This seems like a very simple thing, but I can't figure it out. I have created a adjacency matrix, specifying which nodes are connected to which node, this is a directed graph.
A B C D
A 0 1 0 1
B 0 0 0 0
C 1 0 0 0
D 1 0 1 0
I can read this into igraph with
graph_from_adjacency_matrix(matrix)
I also have a dataframe with specifications for every node f.i.
data.frame(id = c("A", "B","C","D"),
color = c("red","red", "green","deathmetal black"),
shoesize = c(31,32,33,50))
How do I combine this information in 1 graph for plotting purposes?
Upvotes: 1
Views: 680
Reputation: 317
A slightly different approach is using the tidygraph package:
as_tbl_graph(matrix) %>% left_join(data, by=c('name'='id'))
According to Thomas Lin Pederson (creator of tidygraph).
Upvotes: 2
Reputation: 206401
You can merge in node attribute values with set_vertex_attr
If you had
matrix <- structure(c(0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 0L, 0L), .Dim = c(4L, 4L), .Dimnames = list(c("A", "B", "C",
"D"), c("A", "B", "C", "D")))
gg <- graph_from_adjacency_matrix(matrix)
Then you could do
data.frame(id = c("A", "B","C","D"),
color = c("red", "red", "green", "deathmetal black"),
shoesize = c(31,32,33,50), stringsAsFactors=FALSE)
gg <- set_vertex_attr(gg, "color", dd$id, dd$color)
gg <- set_vertex_attr(gg, "shoesize", dd$id, dd$shoesize)
Upvotes: 2