Display name
Display name

Reputation: 4481

Trouble creating lists in R for the networkD3 package

radialNetwork

I'd like to create the radial network above utilizing the R package networkD3. I read the guide here which utilizes lists to create radial networks. Unfortunately my R skills with lists are lacking. They're actually non-existent. Fortunately there's the R4DS guide here.

After reading everything I come up with this code below, to create the diagram above.

library(networkD3)
nd3 <- list(Start = list(A = list(1, 2, 3), B = "B"))
diagonalNetwork(List = nd3, fontSize = 10, opacity = 0.9)

Alas, my attempt fails. And subsequent attempts fail to generate anything that's close to the diagram above. I'm pretty sure it's my list that's wrong. Maybe you can show me the right list and things will start to make sense.

Upvotes: 2

Views: 256

Answers (2)

CJ Yetman
CJ Yetman

Reputation: 8848

If you're like me and the data frame/spreadsheet format is easier to wrap your head around, you could build an easy data frame with your data and then use data.tree functions to convert it to the list/json format...

library(data.tree)
library(networkD3)

source <- c("Start", "Start", "A", "A", "A")
target <- c("A", "B", "1", "2", "3")
df <- data.frame(source, target)

nd3 <- ToListExplicit(FromDataFrameNetwork(df), unname = T)

diagonalNetwork(List = nd3, fontSize = 10, opacity = 0.9)

enter image description here

Upvotes: 2

LocoGris
LocoGris

Reputation: 4480

Jason! The issue here is that the parameter nd3 has a very specific grammar of node name and children. So your code should look like this:

library(networkD3)
nd3 <- list(name = "Start", children = list(list(name = "A",
                                                  children = list(list(name = "1"),
                                                                  list(name = "2"),
                                                                  list(name = "3")
                                                                  )),

                                                 list(name = "B")))
diagonalNetwork(List = nd3, fontSize = 10, opacity = 0.9)

Upvotes: 2

Related Questions