We can also customize edges with a lot of options, and in the same ways as nodes.

Individual configuration

As seen in the introduction, edges must be a data.frame, with at least from and to columns, which make the link with id of nodes. You can add properties simply by adding variables on data.frame. See ?visEdges for available options.

The most used are presented in the following example :

edges <- data.frame(from = sample(1:10,8), to = sample(1:10, 8),
                    
  # add labels on edges                  
  label = paste("Edge", 1:8),
  
  # length
  length = c(100,500),
  
  # width
  width = c(4,1),
  
  # arrows
  arrows = c("to", "from", "middle", "middle;to"),
  
  # dashes
  dashes = c(TRUE, FALSE),
  
  # tooltip (html or character)
  title = paste("Edge", 1:8),
  
  # smooth
  smooth = c(FALSE, TRUE),
  
  # shadow
  shadow = c(FALSE, TRUE, FALSE, TRUE)) 

# head(edges)
#  from to  label length    arrows dashes  title smooth shadow
#    10  7 Edge 1    100        to   TRUE Edge 1  FALSE  FALSE
#     4 10 Edge 2    500      from  FALSE Edge 2   TRUE   TRUE

nodes <- data.frame(id = 1:10, group = c("A", "B"))
visNetwork(nodes, edges, height = "500px", width = "100%")

Global configuration

If you want to set some configuration for all edges, it’s possible in a simpler way, using the visEdges function directly :

nodes <- data.frame(id = 1:4)
edges <- data.frame(from = c(2,4,3,2), to = c(1,2,4,3))

visNetwork(nodes, edges, width = "100%") %>% 
  visEdges(shadow = TRUE,
           arrows =list(to = list(enabled = TRUE, scaleFactor = 2)),
           color = list(color = "lightblue", highlight = "red")) %>%
  visLayout(randomSeed = 12) # to have always the same network           

Finally, you can also combine individual and global options :

nodes <- data.frame(id = 1:4, label = 1:4)
edges <- data.frame(from = c(2,4,3,2), 
                    to = c(1,2,4,3), 
                    dashes = c(TRUE, FALSE))

visNetwork(nodes, edges, width = "100%") %>% 
  visEdges(shadow = TRUE,
           arrows =list(to = list(enabled = TRUE, scaleFactor = 2)),
           color = list(color = "lightblue", highlight = "red")) %>%
  visLayout(randomSeed = 12) # to have always the same network             

Use complex configuration individually

When options are available using a list, you can use it in a data.frame with specific notation like this :

nodes <- data.frame(id = 1:3, 
                    color.background = c("red", "blue", "green"),
                    color.highlight.background = c("red", NA, "red"), 
                    shadow.size = c(5, 10, 15))

edges <- data.frame(from = c(1,2), to = c(1,3),
                    label = LETTERS[1:2], 
                    font.color =c ("red", "blue"), 
                    font.size = c(10,20))

visNetwork(nodes, edges)  

© 2020-2021 DataStorm

Fork me on GitHub