How I can have a node with two colors using VisNetwork?

112 Views Asked by At

Given a dataset, how can I see a node that belongs to different groups, have different colors when they are selected?

Ex:

nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
 group = sample(LETTERS[1:3], 15, replace = TRUE))

edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
 to = trunc(runif(15)*(15-1))+1)
 
nodes$group <- sample(c("group 1", "group 2", "group 1, group 2, group 3"), 
 nrow(nodes), replace = TRUE)
 
visNetwork(nodes, edges) %>% 
 visOptions(selectedBy = list(variable = "group", multiple = TRUE))

How can I give a different color to a node when we select Group 1 and another color when selected for Group 2?

My goal is to be able to select group 1, and node 1 will be X color; and when I select group 3, node 1 will be Y color.

1

There are 1 best solutions below

3
PesKchan On
nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
                    group = sample(LETTERS[1:3], 15, replace = TRUE))

edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
                    to = trunc(runif(15)*(15-1))+1)

nodes$group <- sample(c("group1", "group2", "group1, group2, group3"), 
                      nrow(nodes), replace = TRUE)

visNetwork(nodes, edges) %>% 
  visOptions(selectedBy = list(variable = "group", multiple = TRUE))

New visualization

visNetwork(nodes, edges, width = "100%") %>%                             # arrow "to" for all edges
  visGroups(groupname = "group1", color = "darkblue") %>%    # darkblue for group "A"
  visGroups(groupname = "group2", color = "red")   %>%
  visGroups(groupname = "group3", color = "green")