Search code examples
rd3.jshtmlwidgetsnetworkd3d3-force-directed

Links are directed to top left corner in forceNetwork with networkD3 in R


I am trying to use forceNetwork but the links are pointed to top left and the links are not as expected.

library(tidyverse)
library(networkD3)

nodes <- data.frame(name=c("H2o","H2O Driverless AI ","DAI","This is a Mechanized Operations Tracking System","H2O.ai","H2O","H2O is open-source software for Big Data analysis"),group = 1)

nodes <- nodes %>% rowid_to_column("id")

nodes_d3 <- mutate(nodes, id = id - 1)

links <- data.frame(source=c(0,1,2,0,4,5),target=c(1,2,3,4,5,6))

forceNetwork(Links = links, Nodes = nodes_d3, Source = "source", Target = "target", 
              NodeID = "id",Group = "group", opacity = 0.8,arrows = TRUE, zoom = TRUE)

Expected out is to have h20 as the node 0 then it connects to "H2O Driverless AI " and H2O.ai.Then H2O Driverless AI > DAI. Then DAI>This is a Mechanized Operations Tracking System. H2O.ai> H2O. H2O>H2O is open-source software for Big Data analysis

Please correct if I am going wrong.


Solution

  • Given your data, you should probably use NodeID = "name"

    update:

    You also have to set the Value argument. Here's a reproducible example...

    library(tidyverse)
    library(networkD3)
    
    nodes <- data.frame(name=c("H2o","H2O Driverless AI ","DAI","This is a Mechanized Operations Tracking System","H2O.ai","H2O","H2O is open-source software for Big Data analysis"),group = 1)
    
    nodes <- nodes %>% rowid_to_column("id")
    
    nodes_d3 <- mutate(nodes, id = id - 1)
    
    links <- data.frame(source=c(0,1,2,0,4,5),target=c(1,2,3,4,5,6), value = 1)
    
    forceNetwork(Links = links, Nodes = nodes_d3, Source = "source", Target = "target", 
                 Value = "value", NodeID = "name", Group = "group", opacity = 0.8,arrows = FALSE, zoom = TRUE)
    

    enter image description here