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.