How to highlight the nearest nodes when a group is selected
Asked Answered
S

1

6

When I select a group of nodes from the dropdown, I want the nearest nodes one degree away to also be highlighted. Currently, only the nodes in the group are highlighted.

I thought the highlightNearest with degree = 1 would help, but that only seems to affect when a single node is selected, not when a group is selected.

library(visNetwork)
nb <- 10
nodes <- data.frame(
  id = 1:nb, label = paste("Label", 1:nb),
  group = sample(LETTERS[1:3], nb, replace = TRUE), value = 1:nb,
  title = paste0("<p>", 1:nb, "<br>Tooltip !</p>"), stringsAsFactors = FALSE
)

edges <- data.frame(
  from = c(8, 2, 7, 6, 1, 8, 9, 4, 6, 2),
  to = c(3, 7, 2, 7, 9, 1, 5, 3, 2, 9),
  value = rnorm(nb, 10), label = paste("Edge", 1:nb),
  title = paste0("<p>", 1:nb, "<br>Edge Tooltip !</p>")
)

visNetwork(nodes, edges, height = "500px", width = "100%") %>%
  visOptions(
    selectedBy = "group",
    highlightNearest = TRUE,
    nodesIdSelection = TRUE
  ) %>%
  visLayout(randomSeed = 123)

When I select group A, I see nodes "Label 4", "Label 9", and "Lable 10" highlighted. The edges touching those nodes are also highlighted. Screenshot:

enter image description here

I want the nearest nodes to also be highlighted: "Label 1", "Label 2", "Label 5" and "Label 3" Screenshot with hand-drawn arrows pointed at the extra 4 nodes that should be highlighted:

enter image description here

If I click on nodes "Label 4" & "Label 9" directly, the nearest nodes are highlighted like I want them to be. I want this same highlighting behavior to also work when I select a group. Screenshots:

enter image description here enter image description here

Selfpossession answered 24/8, 2019 at 18:26 Comment(0)
S
0

This is now possible thanks to a recent enhancement by bthieurmel on Github here. Until the enhancement makes it into CRAN, you'll need to install visNetwork from Github, in order to get this to work.

install.packages("devtools")
library(devtools)
install_github("datastorm-open/visNetwork")

Then, update the code from the question, replacing selectedBy = "group" with

selectedBy = list(variable = "group", highlight = TRUE)
Selfpossession answered 3/9, 2019 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.