Search code examples
rvisnetwork

The visIgraphLayout() function overlaps edges between nodes when edges are more than one


I have the the code below which visualizes a network using the r package visNetwork.

library(visNetwork)                  
  id<-c("articaine","benzocaine","etho","esli")
  label<-c("articaine","benzocaine","etho","esli")
  node<-data.frame(id,label)
  
  from<-c("articaine","articaine","articaine","articaine","articaine","articaine","articaine","articaine","articaine")
  to<-c("benzocaine","etho","esli","benzocaine","etho","esli","benzocaine","etho","esli")
  title<-c("SCN1A","SCN1A","SCN1A","SCN2A","SCN2A","SCN2A","SCN3A","SCN3A","SCN3A")
  
  edge<-data.frame(from,to,title)
  
  visNetwork(nodes = node,edge)%>% 
    
    
    visOptions(highlightNearest=T, nodesIdSelection = T) %>%
    
    # Specify that hover interaction and on-screen button navigations are active
    visInteraction(hover = T, navigationButtons = T) %>%
    
    
    visIgraphLayout(randomSeed = 997)

If you remove the last line

%>%
        
        
        visIgraphLayout(randomSeed = 997)

the network visualization is correct

enter image description here

but when added I lose some of the edges.

enter image description here

I need the visIgraphLayout() function as it makes my real network looks nicer and also reproduces much faster. Why does this happen?Possible solutions?


Solution

  • A compromise is to assign the smooth property to from-to combinations with multiple edges, but not to others. In my case this significantly improved the speed of plotting. I am using data.table and visNetwork by the way.

    ##assign smooth property where multiple edges
    edge_list[, N := .N, by = c("from", "to")]
    edge_list[N > 1, smooth := T]
    edge_list[N == 1, smooth := F]
    
    ##plot
    visNetwork(nodes = node_list,
               edges = edge_list[, .(from, to, smooth)]) %>%
    visNodes(physics = F) %>%
    visIgraphLayout(randomSeed = 951)
    

    Sorry for the non-reproducible example, but hopefully useful.