I asked this question earlier about how to change the colour of selected nodes and edges when using the visNetwork package in R. The linked question helps me colour the nodes and edges, however I am making a new question as the previous one doesn't address a new issue Im facing.
In the code below Im creating a simple network. But Im also adding a column to my data that conditionally colours the edges. When I include this column, my edges are initially coloured correctly, however when I select/hover/highlight an edge it doesn't change colour. It just seems to make the edge thicker but keeps the same colour.
Any suggestions as to how I would do this?
library(visNetwork)
# Create example data
nodes <- data.frame(id = 1:4, label = c("Node 1", "Node 2", "Node 3", "Node 4"))
edges <- data.frame(from = c(1, 1, 1, 2, 3), to = c(2, 3, 4, 3, 4), value = c(10, 20, 30, 40, 50))
edges$color <- ifelse(edges$value <= 20, "blue", "red")
# Create visNetwork object with customized node and edge colors
visNetwork(nodes, edges) |>
visNodes(color = list(background = "skyblue", border = "black", highlight = "yellow",
hover = "yellow")) |>
visEdges(color = list(highlight = "yellow",
hover = "yellow"), smooth = FALSE)|>
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)
You seem to have the right idea! The problem is that
edges$color <- ifelse(edges$value <= 20, "blue", "red")sets the initial edge colors, but that conflicts with the dynamic edge coloring invisNetwork.Let me first let you know that I work in R a lot, but am certainly not a programmer so this may not be the best way to go about it. Given that a solution is to just declare both the initial and dynamic colors directly within
VisEdges:I hope this helps!