networkD3 中 sankeyNetwork 的绘图不显示输出,也不在 R 中生成任何 warning/error

plot from sankeyNetwork in networkD3 does not show output neither generates any warning/error in R

我想使用包 in 中的 sankeyNetwork() 生成桑基图来可视化不同区域的运动。我试图尽可能完美地模仿一些例子。但是当我运行 函数sankeyNetwork 时,没有生成任何输出。最重要的是,R 不显示任何警告、错误等。因此,我无法真正检查我是否犯了错误(很明显,因为没有生成情节)以及如何修复它们。我提供了一个示例 df 和下面的代码。

library(networkD3)

nodes <- data.frame(area = c("a", "b", "c", "d", "e", "f", "g", 
                            "h", "i", "j", "k", "l", "m", "n")) 

links2 <- data.frame(source = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
                                 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 12, 13, 13), 
                     target = c(2, 8, 10, 11, 13, 0, 4, 5, 6, 7, 10, 11, 13, 0, 4, 9, 10, 12, 13, 0, 5, 6, 7, 10, 11, 13, 7, 10, 12, 
                     0, 10, 11, 12, 13, 8, 9, 10, 11, 12, 13, 9, 10, 13, 10, 12, 13, 0, 11, 12, 13, 0, 14, 0, 0), 
                     value  = c(14, 4, 6, 23, 3, 6, 36, 3, 4, 4, 3, 12, 3, 24, 3, 6, 19, 3, 9, 3, 6, 3, 11, 9, 3, 22, 3, 3, 10, 3, 4, 
                                3, 3, 9, 12, 5, 16, 13, 3, 10, 3, 4, 9, 7, 4, 4, 77, 4, 6, 6, 27, 3, 3, 3))

sankeyNetwork(Links = links2, Nodes = nodes,
              Source = "source", Target = "target",
              Value = "value", NodeID = "area",
              fontSize= 12, nodeWidth = 30) 

您在 links2 数据框中引用了 15 个唯一节点,但在 nodes 数据框中只有 14 个唯一节点。

length(unique(c(links2$source, links2$target)))
# [1] 15
length(nodes$area)
# [1] 14

如果您添加另一个节点,它将起作用...

library(networkD3)

nodes <- data.frame(area = c("a", "b", "c", "d", "e", "f", "g", 
                             "h", "i", "j", "k", "l", "m", "n", "o")) 

links2 <- data.frame(source = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
                                5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 12, 13, 13), 
                     target = c(2, 8, 10, 11, 13, 0, 4, 5, 6, 7, 10, 11, 13, 0, 4, 9, 10, 12, 13, 0, 5, 6, 7, 10, 11, 13, 7, 10, 12, 
                                0, 10, 11, 12, 13, 8, 9, 10, 11, 12, 13, 9, 10, 13, 10, 12, 13, 0, 11, 12, 13, 0, 14, 0, 0), 
                     value  = c(14, 4, 6, 23, 3, 6, 36, 3, 4, 4, 3, 12, 3, 24, 3, 6, 19, 3, 9, 3, 6, 3, 11, 9, 3, 22, 3, 3, 10, 3, 4, 
                                3, 3, 9, 12, 5, 16, 13, 3, 10, 3, 4, 9, 7, 4, 4, 77, 4, 6, 6, 27, 3, 3, 3))

sankeyNetwork(Links = links2, Nodes = nodes,
              Source = "source", Target = "target",
              Value = "value", NodeID = "area",
              fontSize= 12, nodeWidth = 30)