带有 R 库 networkD3 的桑基图不显示颜色

Sankey Diagram with R library networkD3 does not show colors

我正在使用 R 的 networkD3 库来创建 Sankey 网络。虽然这对我来说效果很好,但我现在遇到了分配属性 "NoteID" and/or "NoteGroup" 以分组和分配颜色的问题,如 https://christophergandrud.github.io/networkD3/#sankey[=15= 所示]

以下代码展示了创建 Sankey 图的 4 个示例,只有 "Sankey4" 按设计工作,即没有颜色:

library(networkD3)
#Unique list of nodes
my_nodes = structure(list(name = c("HawaiTEST", "AMSVOASMPP01", "App1", 
                                   "Transfer", "Transferred_tel__63null_",
                                   "Transferred_tel__631100107_", 
                                   "AMSVOASMPP02",
                                   "Transferred_tel__631100108_",
                                   "Transferred_tel__631100106_", 
                                   "Transferred_tel__631100104_",
                                   "Transferred_tel__631100105_", 
                                   "FarEndDisconnect",
                                   "FarEndDisconnect_Hangup", "DutchAOS",
                                   "SwedenAOS", 
                                   "Transferred_tel__63000_")), class =
c("tbl_df", "tbl", "data.frame"
  ), row.names = c(NA, -16L), .Names = "name")

# Network
my_links = structure(list(key = c("0_1", "0_6", "1_13", "1_14", "1_2", "11_12", 
                                  "13_11", "13_3", "14_11", "14_3", "2_11",
                                  "2_3", "3_10", "3_15", 
                                  "3_4", "3_5", "3_7", "3_8", "3_9", "6_13",
                                  "6_2"), source = c(0L, 
                                  0L, 1L, 1L, 1L, 11L, 13L, 13L, 14L, 14L, 2L,
                                  2L, 3L, 3L, 3L, 
                                  3L, 3L, 3L, 3L, 6L, 6L), target = c(1L, 6L,
                                  13L, 14L, 2L, 12L, 
                                  11L, 3L, 11L, 3L, 11L, 3L, 10L, 15L, 4L, 5L,
                                  7L, 8L, 9L, 13L, 
                                  2L), total = c(38L, 36L, 4L, 3L, 31L, 6L, 2L,
                                  5L, 1L, 2L, 3L, 
                                  61L, 11L, 1L, 12L, 11L, 11L, 11L, 11L, 3L,
                                  33L)), class = c("tbl_df", 
                          "tbl", "data.frame"), row.names = c(NA, -21L), .Names
= c("key", 
    "source", "target", "total"))


# NOT WORKING using  "NodeID", or "NodeGroup"
sankey1 = sankeyNetwork(Links =my_links, Nodes = my_nodes, Source =
                           "source", Target = "target", Value = "total", units =
                           "calls", NodeID = "name")

sankey2 = sankeyNetwork(Links =my_links, Nodes = my_nodes, Source =
                           "source", Target = "target", Value = "total", units =
                           "calls", NodeGroup = "name")
sankey2

# NOT WORKING using ColourScale (diagram is displayed, grey scale though)
ColourScale <- 'd3.scale.ordinal()
            .domain(["lions", "tigers"])
           .range(["#FF6900", "#694489"]);'
sankey3 = sankeyNetwork(Links =my_links, Nodes = my_nodes, Source =
                           "source", Target = "target", Value = "total", units =
                           "calls", colourScale = JS(ColourScale))
sankey3

# WORKING! 

sankey4 = sankeyNetwork(Links =my_links, Nodes = my_nodes, Source =
                           "source", Target = "target", Value = "total", units =
                           "calls")
sankey4

"Sankey1" 尝试使用 "NoteID" 在上面引用的网络示例中使用的方式,但是,这样做会导致图表根本不显示; "Sankey2" 的效果相同。 "Sankey4" 无论配色方案定义如何,都以灰色显示。

我还查看了两者生成的 html 代码,我的 R 代码生成 "Sankey1" 以及 https://christophergandrud.github.io/networkD3/#sankey 上使用的代码。显然,关于组有区别:

HTML 来自 "Sankey1":

"group":{"name":["HawaiTEST", ...
...
"options":{"NodeID":1,"NodeGroup":"name","LinkGroup":null,

HTML 摘自网络示例:

"group":["Agricultural 'waste'","Bio-conversion", ...
...
"options":{"NodeID":"name","NodeGroup":"name","LinkGroup":null

更改 "Sankey1" 的输出 html 以反映 Web 示例的输出解决了问题,"Sankey1" 使用默认颜色模式显示。

我现在正在碰壁,试图了解我正在使用的数据的行为。 sankey 函数不依赖于强制列表输入;实际上,我已经将来自网站的示例数据集拆分为两个数据框(节点、链接),这确实会生成与 Web 示例中相同的带有颜色的桑基图。因此,我的示例中的输入数据一定有问题......我想......任何帮助将不胜感激! 谢谢 奥利

也许,我误会了,但是使用 CRAN 或 Github 版本,sankey1 为我生成了以下内容,节点按名称按预期着色。

如果我们想使用NodeGroup,我们可以这样做。

# make up a group based on the first two characters
#  of node name
my_nodes$group <- substr(my_nodes$name,1,2)
# now use our new group for group colors
sankeyNetwork(
  Links =my_links, Nodes = my_nodes, Source = "source",
  Target = "target", Value = "total", NodeID = "name",
  units = "calls",
  NodeGroup = "group"
)

如果我们想提供自定义色标,我们可以这样做。

sankeyNetwork(
  Links =my_links, Nodes = my_nodes, Source = "source",
  Target = "target", Value = "total", NodeID = "name",
  units = "calls",
  NodeGroup = "group",
  colourScale = "d3.scale.category10()"
)

对于颜色的自定义分配,我们可以扩展前面的示例并砍掉 d3.scale.category* 函数。

sankeyNetwork(
  Links =my_links, Nodes = my_nodes, Source = "source",
  Target = "target", Value = "total", NodeID = "name",
  units = "calls",
  NodeGroup = "group",
  colourScale = sprintf(
    "d3.scale.category10().range(%s).domain(%s)",
    jsonlite::toJSON(substr(topo.colors(length(unique(my_nodes$group))),1,7)),
    jsonlite::toJSON(unique(my_nodes$group))
  )
)

我遇到了类似的问题。我通过减少节点总数来解决它(通过仅过滤高于某个值的边)。