visNetwork
visNetwork copied to clipboard
highlightNearest - When a node is selected, highlight only edges moving *from* that selected node
Hi, what I am looking to do is when I select a given node, highlight only the edges from that selected node (as as well as the nodes connected with those edges). I don't want to highlight any secondary edges, nor do I want to highlight any edges connecting back to the selected node.
Creating a simple example:
library(visNetwork)
nodes <- data.frame(id = 1:10)
edges <- data.frame(from = c(1, 2, 3, 4, 5, 3, 6, 7, 8, 9, 10, 6, 9, 4, 7, 1, 5, 9, 3),
to = c(2, 3, 1, 2, 4, 4, 3, 2, 4, 1, 7, 8, 10, 3, 3, 8, 3, 7, 7))
edges$arrows <- "to"
edges$width <- 1
visNetwork(nodes, edges, width = "100%") %>%
visOptions(highlightNearest = list(enabled = TRUE, algorithm = "hierarchical",
degree = list(from = 0, to = 1)),
nodesIdSelection = list(enabled = TRUE))
For instance, when I select node 7, I only want to highlight the edge moving from 7 to 2 and the edge moving from 7 to 3. I don't want highlighted the edge moving from 2 to 3, nor do I want highlighted the edge moving from 3 to 7. I still want nodes 2 and 3 highlighted though. In addition, I am looking for all the node labels that aren't connected through those "from" edges to not be visible (1, 6, 4, 5).
My actual data is slightly more complex, and I am interested when selecting a node to visualize only the possible moves from the selected node. These additional highlights make the network too busy to look at.
I have also tried with algorithm = "all" and get close, but nodes 2 and 3 aren't highlighted now, and there are also extra node labels showing that I don't want:
visNetwork(nodes, edges) %>% visOptions(highlightNearest = list(enabled = TRUE, algorithm = "all",
degree = list(from = 0, to = 1)),
nodesIdSelection = list(enabled = TRUE))
Is there a way to do this in this package?
Thanks a lot.