A similar question has been asked here, but I cant seem to adapt it to my code. What I'm trying to do is change the colour of the nodes and edges using the visNetwork
package in R. I want to apply this colour change when either
1: the mouse is hovering over a node/edge and
2: when a node or edge is clicked on.
Below is some code to create a very simple network.
# Load required packages
library(visNetwork)
# Create example data
nodes <- data.frame(id = 1:3, label = c("Node 1", "Node 2", "Node 3"))
edges <- data.frame(from = c(1, 1, 2), to = c(2, 3, 3), value = c(10, 20, 30))
# Create visNetwork object with customized node and edge colors
visNetwork(nodes, edges) |>
visNodes(color = list(background = "red",
border = "black")) |>
visEdges(color = "blue", smooth = FALSE)|>
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)
At the moment, I have the nodes coloured red and when hovering over/clicking the node with the mouse, it automatically changes colour to a blue. I would like to control that colour somehow. Also, when selecting an edge, it just seems to make the edge thicker but keeps the same colour. I want to try and change the edge colour when selecting it.
I have tried playing with the visOptions
, visEdges
, and visNodes
arguments, but I just can't seem to figure it out.
From ?visEdges
(or ?visNodes
), argument color
:
Named list or String. Default to named list. Color information of the edge in every situation. Can be 'rgba(120,32,14,1)', '#97C2FC' (hexa notation on 7 char without transparency) or 'red'.
- "color" : String. Default to '#848484. The color of the edge when it is not selected or hovered over (assuming hover is enabled in the interaction module).
- "highlight " : String. Default to '#848484'. The color the edge when it is selected.
- "hover" : String. Default to '#848484'. The color the edge when the mouse hovers over it (assuming hover is enabled in the interaction module).
- ...
# Load required packages
library(visNetwork)
# Create example data
nodes <- data.frame(id = 1:3, label = c("Node 1", "Node 2", "Node 3"))
edges <- data.frame(from = c(1, 1, 2), to = c(2, 3, 3), value = c(10, 20, 30))
# Create visNetwork object with customized node and edge colors
visNetwork(nodes, edges) |>
visNodes(color = list(background = "red", border = "black", highlight = "yellow",
hover = "yellow")) |>
visEdges(color = list(color = "blue", highlight = "yellow",
hover = "yellow"), smooth = FALSE)|>
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)