datastorm-open / visNetwork

R package, using vis.js library for network visualization
Other
545 stars 126 forks source link

visOptions selectedBy to show only connected edges (like highlightNearest = list(algorithm = "hierarchical")) #319

Closed av96-HW closed 5 years ago

av96-HW commented 5 years ago

I am working on a series of graphics depicting subnetworks that are each viewed through the use of selectedBy.

One problem I am encountering is edges connecting to unhighlighted nodes that are not part of the subnetwork. I encountered this issue with highlightNearest, where it is possible to resolve the issue through the use of algorithm = "hierarchical". Is something similar possible for selectedBy? It makes no sense to me that edges that are not part of the subnetwork are highlighted

Code below and data attached.

Thanks very much for your help!

example

rm(list = ls()); cat("\014")

pacman::p_load(tidyverse, igraph, visNetwork)

# visNetwork function
visualisation <- function(nodeData = Nodes, linkData = Links,
                          defaultColours = defaultColours, plotTitle) {

  x = nodeData$subnetwork %>% unique
  x = x[!str_detect(x, "Other")][!str_detect(x, "other")]

  visNetwork(nodeData, linkData, 
             height = "750px", width = "100%",
             background = "white", 
             main = list(text = plotTitle, 
                         style = 
                           "font-family:Arial;
                         text-align:Center;
                         font-size: 21px")) %>%

    visNodes(color = list(border = defaultColours$dark,
                          background = defaultColours$light,
                          highlight = list(background = defaultColours$highlight,
                                           border = defaultColours$dark),
                          hover = list(background = defaultColours$highlight,
                                       border = defaultColours$dark))) %>%

    visEdges(arrows = "to;from",
             color = list(color = defaultColours$dark,
                          select = defaultColours$dark,
                          highlight = defaultColours$dark,
                          hover = defaultColours$dark)) %>%

    visIgraphLayout(type = "full", 
                    layout = "layout_with_sugiyama", 
                    layers = nodeData$level,
                    vgap = 25,
                    physics = FALSE) %>%

    visInteraction(dragNodes = TRUE, 
                   dragView = TRUE, 
                   zoomView = TRUE,
                   hover = TRUE, 
                   tooltipDelay = 0, 
                   multiselect = TRUE, 
                   selectConnectedEdges = TRUE, # Turn off to hide lines when clicked

                   tooltipStyle = 
                   "position:fixed;
                   visibility:hidden;
                   padding:5px;
                   white-space:nowrap;
                   font-family:Arial;
                   font-size:18px;
                   background-color:transparent") %>%

    visOptions(highlightNearest = list(enabled = TRUE, 
                                       degree = list(from = 3, to = 3), 
                                       algorithm = "hierarchical",
                                       hideColor = defaultColours$hideColor), 
               selectedBy = list(variable = "subnetwork", 
                                 style = "font-family:Arial",
                                 hideColor = defaultColours$hideColor,
                                 selected = x)
    ) 

}

# Data
dl = readRDS("questionData.RDS")

# View network
list(dark = "#01acde", 
     light = "#b3eeff", 
     highlight = "#e6f9ff", 
     hideColor = "rgba(200,200,200,0.1)") %>% 

  visualisation(dl$nodes, dl$links,
                defaultColours = .,
                plotTitle =  "Water subnetwork")

questionData.zip

bthieurmel commented 5 years ago

Available on dev version on github. It's normally now possible to combine highlightNearest and selectedBy. You have to :

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)

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