rnetwork-programmingvisnetwork

VisNetwork in R: Adjust length of edges


Is there a way to adjust the length of the edges in VisNetwork so that they are overall a bit longer? Some arrows are a bit hard to see right now especially with the thicker edges.

enter image description here

This is my edge list and nodes list

nodes_t <- structure(list(ahaid = c("6140002", "6140005", "6140007", "6140008", 
"6140010", "6140011", "6140012", "6140013", "6140016", "6140017", 
"6140065", "6140080", "6140090", "6140215", "6140255", "6140270", 
"6140310", "6140420", "6140428", "6140430", "6140465", "6140583", 
"6140620", "6140630", "6140690", "6140780", "6140850", "6140900", 
"6140923", "6140980", "6141010", "6141020", "6141095", "6141110", 
"6141130", "6141170", "6141300", "6141355", "6141395", "6141410", 
"6141430", "6141450", "6141500", "6141530", "6141570", "6141630", 
"6141640", "6141660", "6141705", "6141720", "6141890", "6141900", 
"6141940", "6141955", "6142000", "6142200", "6142280", "6142350"
), weight = c(129L, 67L, 72L, 111L, 193L, 56L, 32L, 601L, 406L, 
151L, 19L, 56L, 25L, 1909L, 22L, 6L, 38L, 6L, 264L, 1416L, 169L, 
133L, 98L, 87L, 387L, 53L, 63L, 101L, 91L, 32L, 11L, 55L, 456L, 
34L, 225L, 62L, 31L, 158L, 185L, 24L, 6L, 43L, 47L, 112L, 93L, 
94L, 12L, 46L, 175L, 161L, 30L, 452L, 234L, 379L, 86L, 19L, 85L, 
135L)), row.names = c(NA, -58L), class = "data.frame")

edges_t <- structure(list(from = c("6140002", "6140008", "6140010", "6140012", 
"6140013", "6140013", "6140013", "6140016", "6140017", "6140080", 
"6140215", "6140215", "6140215", "6140215", "6140215", "6140215", 
"6140215", "6140215", "6140215", "6140215", "6140215", "6140215", 
"6140428", "6140430", "6140430", "6140430", "6140923", "6141020", 
"6141095", "6141095", "6141170", "6141660", "6141940"), to = c("6140430", 
"6141095", "6140215", "6140430", "6140630", "6141450", "6141720", 
"6140215", "6140690", "6140215", "6140310", "6140428", "6140430", 
"6140583", "6140690", "6141095", "6141130", "6141395", "6141530", 
"6141630", "6141900", "6142000", "6140690", "6140850", "6141530", 
"6141900", "6141130", "6141955", "6141300", "6142350", "6141355", 
"6141955", "6141955"), weight = c(19L, 22L, 7L, 8L, 9L, 17L, 
9L, 15L, 19L, 12L, 22L, 7L, 27L, 90L, 14L, 20L, 13L, 32L, 9L, 
11L, 34L, 8L, 7L, 11L, 17L, 16L, 6L, 16L, 7L, 5L, 10L, 17L, 18L
)), row.names = c(NA, -33L), class = "data.frame")

This is my code

vis.nodes <- nodes_t
vis.links <- edges_t
vis.nodes$borderWidth <- 2 
vis.nodes <- vis.nodes %>% mutate(size = weight/16)

vis.links$width <- 1+edges_t$weight/4 
vis.links$arrows <- "to" 

library('visNetwork') 
p <- visNetwork(vis.nodes, vis.links, width="400%", height="1600px")

Solution

  • You could use the visEdges function with argument length to change the length of the edges. Since I can't reproduce your plot, I created a reproducible example:

    library(visNetwork)
    library(dplyr)
    # Data
    nodes <- data.frame(id = 1:2)
    edges <- data.frame(from = 1, to = 2)
    
    # Plot with length 100
    visNetwork(nodes, edges) %>%
      visEdges(arrows = 'to', length = 100) 
    

    # Plot with length 400
    visNetwork(nodes, edges) %>%
      visEdges(arrows = 'to', length = 400) 
    

    Created on 2022-12-18 with reprex v2.0.2

    As you can see the first plot has an edge length of 100 pixels and the second plot has an edge length of 400 pixels.


    If you want to have variable lengths, you could add an variable length to your edge dataframe like this:

    library(visNetwork)
    library(dplyr)
    # Data
    nodes <- data.frame(id = 1:2)
    edges <- data.frame(from = 1, to = 2, length = c(100, 400))
    
    # Plot
    visNetwork(nodes, edges) %>%
      visEdges(arrows = "to")
    

    Created on 2022-12-18 with reprex v2.0.2