I'm trying to color edges in a graph according to an edge attribute using visNetwork, but they keep coming out the same color as the nodes. What am I doing wrong?
Code:
edges$color <- edges$agreement_num
visNetwork(nodes, edges) %>%
visEdges(arrows = 'from', smooth =T) %>%
visOptions(selectedBy= "com") %>%
visNodes(label = TRUE,scaling = list(label = list(min = 30, max = 50))) %>%
visIgraphLayout(layout = "layout_nicely") %>%
Reproducible example:
nodes <- structure(list(id = c("United States of America", "Canada", "Mexico",
"Peru", "Colombia", "Honduras", "Guatemala", "Nicaragua", "Nigeria",
"Belize"), com = c("5", "5", "5", "5", "5", "5", "5", "5", "5",
"5"), label = c("United States of America", "Canada", "Mexico",
"Peru", "Colombia", "Honduras", "Guatemala", "Nicaragua", "Nigeria",
"Belize"), font.size = c(30, 30, 30, 30, 30, 30, 30, 30, 30,
30), group = c("5", "5", "5", "5", "5", "5", "5", "5", "5", "5"
)), row.names = c("United States of America", "Canada", "Mexico",
"Peru", "Colombia", "Honduras", "Guatemala", "Nicaragua", "Nigeria",
"Belize"), class = "data.frame")
edges <- structure(list(from = c("Canada", "Canada", "United States of America",
"Mexico", "United States of America", "Mexico", "United States of America",
"Canada", "Mexico", "United States of America"), to = c("Antigua and Barbuda",
"Barbados", "Barbados", "Belize", "Belize", "Canada", "Canada",
"China, Taiwan Province of", "China, Taiwan Province of", "China, Taiwan Province of"
), weight = c(171966510.34557, 3894809879.52684, 190168793161.723,
229417909.693992, 56692662824.8982, 713710750.313289, 6240520849382.04,
286476683134.553, 928907970.889569, 14741754565243.3), agreement_num = c(1,
3, 45, 32, 2, 185L, NA, 3, 6, 45), value = c(171966510.34557,
3894809879.52684, 190168793161.723, 229417909.693992, 56692662824.8982,
713710750.313289, 6240520849382.04, 286476683134.553, 928907970.889569,
14741754565243.3)), row.names = c(NA, 10L), class = "data.frame")
You need to assign R colour names, hex values, or a combination of both to your color column. You can do this manually, or by using the
palette()function. Here's how to do it manually:Screencap of result: