无法在 R 中为 networkD3 包创建列表

Trouble creating lists in R for the networkD3 package

我想使用 R 包 networkD3 创建上面的径向网络。我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.

阅读完所有内容后,我想出下面的代码来创建上面的图表。

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

唉,我的尝试失败了。随后的尝试无法生成任何接近上图的内容。我很确定是我的清单错了。也许你可以告诉我正确的列表,然后事情就会开始变得有意义了。

杰森! 这里的问题是参数 nd3 具有非常具体的节点名称和子节点语法。所以你的代码应该是这样的:

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)

如果您像我一样并且数据 frame/spreadsheet 格式更容易理解,您可以使用您的数据构建一个简单的数据框,然后使用 data.tree 函数将其转换到 list/json 格式...

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)