Change edge color of a visnetwork based on a scalar value

Viewed 381

I'm aware of changing the color of a node in VisNetwork based on a given scalar value. But how can one change the color of an edge (irrespective of node color) based on a given scalar value.

Function I wrote to change color of node given a scalar value (per node):

cap_s <- c(0.7, 0.5, NA, 0.8, 0.9, 
            0.7, 0.5, 0.2, 0.8, 0.9,
            0.7, NA, 0.2, NA, 0.9,
            0.7, 0.5, 0.2, 0.8, 0.9)

node_color <- function(s_cap){
  output = c()
  for (val in (1:length(s_cap))) {
    if (is.na(s_cap[val]) ==  FALSE) {
      if (s_cap[val] > 0.8) {
        output[val] <- "green"
      } else if (s_cap[val] < 0.5){
        output[val] <- "red"
      } else if ((s_cap[val] >= 0.5)&(s_cap[val] <= 0.8)){
        output[val] <- "lightblue"
      } 
    }
    else {
        output[val] <- "grey"
    }
  }
  return(output)
}

My network:

Vis_net <- function(){
  nodes = node_data()
  nodes <- nodes %>% mutate(cap = cap_sensor) %>% mutate(color = node_color(cap_s))
  visNetwork(nodes, edges(), main = "Overall Structure")  %>% 
    visEdges(arrows = "to") %>% 
    visHierarchicalLayout(direction = "LR", levelSeparation = 200) %>%
    visLegend(addNodes = list(
      list(label = ">80%", shape = "circle", color = "green"),
      list(label = ">50% & <80%" , shape = "circle", color = "lightblue"),
      list(label = "<50%", shape = "circle", color = "red"),
      list(label = "Unused", shape = "circle", color = "grey")), 
    useGroups = FALSE,
    )
}

Is there a way to change the value of the edges based on a given scalar value per edge (like the way I did with the node_color function)? I have 32 edges in the network.

0 Answers
Related