SankeyNetwork 在 R 中显示空图(networkD3 包)

问题描述 投票:0回答:0

我在尝试使用 R 中的 networkD3 包绘制 Sankey 图时遇到问题。该图显示为空,没有任何可见的流或节点。我仔细检查了我的数据结构和代码,并尝试将“源”和“目标”列定义为整数和数字,但这两种方法都无法正确显示绘图。这是我目前使用的代码:

# Data for the "links" dataset
links <- data.frame(
  source = c(0, 1, 2, 4, 5, 7, 8, 1, 1, 4, 4, 7, 7, 2, 2, 5, 5, 8, 8),
  target = c(1, 2, 3, 5, 6, 8, 9, 8, 5, 2, 8, 2, 5, 6, 9, 3, 9, 3, 6),
  value = c(10, 5, 7, 46, 5, 14, 7, 5, 0, 2, 4, 1, 2, 0, 0, 22, 17, 14, 1)
)

# Data for the "nodes" dataset
nodes <- data.frame(
  name = c("specifically correct 1", "specifically correct 2", "specifically correct 3",
           "incorrect 1", "incorrect 2", "incorrect 3",
           "non-specifically correct 1", "non-specifically correct 2", "non-specifically correct 3")
)

# Plot
sankeyNetwork(Links = links, Nodes = nodes, Source = "source", Target = "target", Value = "value",NodeID = "name", units = "Value", fontSize = 12, nodeWidth = 30)

我尝试了不同的参数组合,包括调整字体大小和节点宽度,但结果图仍然是空的。我怀疑数据结构或内容可能存在问题。对于数据集的结构和内容,还有什么其他重要的事情需要考虑吗?使用以下公共 JSON 数据正确显示 SankeyPlot:

#Load energy projection data
URL <- paste0(
        "https://cdn.rawgit.com/christophergandrud/networkD3/",
        "master/JSONdata/energy.json")
Energy <- jsonlite::fromJSON(URL)
# Plot
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             units = "TWh", fontSize = 12, nodeWidth = 30)

对于如何解决此问题的任何见解或建议,我将不胜感激!!

r plot visualization sankey-diagram networkd3
© www.soinside.com 2019 - 2024. All rights reserved.