2

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.

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Display name
  • 4,153
  • 5
  • 27
  • 75

2 Answers2

2

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)
LocoGris
  • 4,432
  • 3
  • 15
  • 30
2

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

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • 1
    This method does seem a lot more convenient and is what I'll probably end up using. But for now I'm going to try to learn lists and how to do this with base R and without loading additional packages. Still upvoted this though. Thanks. – Display name Feb 16 '19 at 16:34
  • 1
    You could write a recursive function in base R to do the same thing, but then you get back into mind bending territory. ;-) – CJ Yetman Feb 16 '19 at 18:58