Reputation: 1875
I would like to fill all the nodes in edges$from
that belong to the same groups$group
with the same color. How can I do that? Thanks for help!
library(tidyverse)
library(igraph)
library(GGally)
edges <- data.frame(from = c("A", "D", "B", "C", "A", "F", "F", "H", "I", "K", "J", "B", "I", "B", "L", "L"),
to = c("E", "A", "A", "A", "G", "G", "C", "G", "G", "J", "K", "I", "B", "G", "K", "K"))
groups <- data.frame(node = "A", "B", "D", "E", "C", "F", "H", "G", "I", "K", "J", "L",
group = "1", "1", "1", "E", "2", "2", "2", "2", "2", "3", "3", "3")
g <- graph_from_data_frame(edges, directed = TRUE)
ggnet2(igraph::simplify(g), label = TRUE)
Upvotes: 1
Views: 45
Reputation: 102309
First of all, you should use c()
to wrap the columns in the data.frame groups
, i.e.,
groups <- data.frame(
node = c("A", "B", "D", "E", "C", "F", "H", "G", "I", "K", "J", "L"),
group = c("1", "1", "1", "E", "2", "2", "2", "2", "2", "3", "3", "3")
)
Then, without ggnet2
, functions in igraph
are sufficient to help .you to reach the goal. For example
edges %>%
graph_from_data_frame(directed = TRUE) %>%
simplify() %>%
set_vertex_attr(
name = "grp",
value = with(groups, factor(group[match(names(V(.)), node)]))
) %>%
plot(vertex.color = V(.)$grp)
Upvotes: 0