network maps with R - highlighting a user-defined path when selecting a node

Viewed 226

I'm using visNetwork in R to generate a hierarchal network map. I want the paths of which get highlighted upon node selection to follow a user-defined rule (EG, adding to the dataset.) Please follow my example below.

library(tidyverse)
library(visNetwork)

nodes = data.frame(id = 1:7, level = c(1, 2, 3, 3, 4, 2,1))
edges = data.frame(from = c(1, 2, 2, 4, 6,7,7), to = c(2, 3, 4, 5, 4,6,2))

visNetwork(nodes, edges) %>% 
visHierarchicalLayout() %>% 
visEdges(arrows = "to") %>% 
visOptions(highlightNearest = list(enabled = TRUE, algorithm = "hierarchical",
                                   degree = n_distinct(nodes$level))) %>%
visHierarchicalLayout(direction = "DU") 

This code produces the below network map

                                   

When selecting node 1, I get the left picture below; When selecting node 7, i get the right picture below. IE, the highlighting follows all the arrows to the top.

I want to find a way in which I can add to the nodes/edges data to user-define the behaviour of the highlighting? EG, when selecting 7 (in the diagram on the right above), it excludes highlighting 2 and 3.

I see two possible options:
1. Adding to the data so that it produces two types of edges; 1 that is just for show and the other that's used for highlighting paths as well being shown.
2. Add extra columns to the data so that it somehow knows how to path highlighting when selecting each node

Open to other suggestions on the options or even packages.

Thanks

0 Answers
Related