Differences in visNetwork graph appearance with more than 1000 nodes

108 Views Asked by At

I'm using the visNetwork package in R to create interactive network graphs. However, I've noticed that the appearance of my graph changes significantly as soon as I include more than 1000 nodes.

nice rendering with <= 1000 nodes: nice rendering with <= 1000 nodes

nodes clustered together with >= 1001 nodes: nodes clustered together with >= 1001 nodes

Here's the code I am using:

    visNetwork(nodes, edges, height = "1250px", width = "100%") %>%
      visIgraphLayout %>%
      visOptions(
        highlightNearest = list(
          enabled = T,
          degree = 1,
          hover = F
        ),
        nodesIdSelection = TRUE
      ) %>%
      visEdges(
        scaling = list(max = 5),
        color = list(opacity = 0.6),
        smooth = TRUE
      ) %>%
      visPhysics(
        solver = "forceAtlas2Based",
        forceAtlas2Based = list(springConstant = 0.785),
        stabilization = F
      ) %>%
      visNodes(font = list(size = 14, strokeWidth = 4),
               size = 11) %>%
      visConfigure(enabled = T) %>%
      visInteraction(navigationButtons = TRUE, multiselect = TRUE) %>%
      visLayout(randomSeed = 1) %>%  
      visSave("/some/path.html")

And here some example nodes and edges:

| id | label | title | centrality | shape | group |
| ---:|:--- |:--- | ---:|:--- | ---:|
| 1 | a | \<chr \[1\]\> | 31748 | dot | 1 |
| 2 | b | \<chr \[1\]\> | 18587 | dot | 5 |
| 3 | c | \<chr \[1\]\> | 11693 | dot | 3 |
| 4 | d | \<chr \[1\]\> | 12504 | dot | 5 |
| 5 | e | \<chr \[1\]\> | 18515 | dot | 2 |
| 6 | f | \<chr \[1\]\> | 16438 | dot | 3 |
| 7 | g | \<chr \[1\]\> | 3903 | dot | 5 |
| 8 | h | \<chr \[1\]\> | 11741 | dot | 5 |
| 9 | i | \<chr \[1\]\> | 10497 | dot | 4 |
| 10 | j | \<chr \[1\]\> | 10254 | dot | 1 |
| from | to | value |
| ---:| ---:| ---:|
| 1 | 1 | 12110. |
| 1 | 199 | 9165. |
| 1 | 322 | 9029. |
| 1 | 480 | 5059. |
| 1 | 150 | 3841. |
| 1 | 92 | 2598. |
| 1 | 468 | 2256. |
| 1 | 696 | 2086. |
| 1 | 897 | 2072. |
| 1 | 137 | 1640. |

By gradually adding more nodes, I have seen that it does not depend on the concrete node I add: as soon as it is > 1000 nodes, this problem occurs. I have also tried all the algorithms available in visNetwork (under physics -> solver), they all result in the same problem. I encounter the problem on different, not overlapping data sets (although the kind of data is similar)

1

There are 1 best solutions below

0
codingtim On

Okay, I have found the solution. It is the default layout algorithm of the visIgraphLayout function. If I change it to layout_with_fr, it works!

layout_nicely tries to choose an appropriate layout function for the supplied graph, and uses that to generate the layout. The current implementation works like this: If the graph has a graph attribute called ‘layout’, then this is used. If this attribute is an R function, then it is called, with the graph and any other extra arguments. Otherwise, if the graph has vertex attributes called ‘x’ and ‘y’, then these are used as coordinates. If the graph has an additional ‘z’ vertex attribute, that is also used. Otherwise, if the graph is connected and has less than 1000 vertices, the Fruchterman-Reingold layout is used, by calling layout_with_fr.