我想生成一个Sankey图,以使用networkd3中的软件包r中的sankeyNetwork()
可视化到不同区域的运动。我试图尽可能地模仿一些例子。但是,当我运行函数sankeyNetwork
时,不会生成任何输出。最重要的是,R不会显示任何警告,erros等。因此,我无法真正检查我是否犯了错误(显然是因为没有生成图)以及如何解决它们。我提供了一个示例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)
答案 0 :(得分:1)
您在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)