具有R库networkD3的Sankey图不显示颜色

编程入门 行业动态 更新时间:2024-10-23 15:30:43
本文介绍了具有R库networkD3的Sankey图不显示颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用R的networkD3库来创建Sankey网络.尽管这对我来说效果很好,但我现在遇到了一个问题,即分配属性"NoteID"和/或"NoteGroup"进行分组和分配颜色,如 christophergandrud.github.io/networkD3/#sankey

I am using the networkD3 library for R in order to create Sankey networks. While this works pretty well for me, I have now encountered an issue with assigning the attributes "NoteID" and/or "NoteGroup" to group and allocate colors as shown in christophergandrud.github.io/networkD3/#sankey

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

The following code shows 4 examples of creating a Sankey diagram, only "Sankey4" works as designed, i.e., no colors:

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"来使用上面引用的Web中示例中的示例,但是这样做会导致根本不显示该图.对"Sankey2"具有相同的效果.不论配色方案定义如何,"Sankey4"均显示为灰色.

"Sankey1" tries using "NoteID" the way it is used at the example from the web referenced above, however, doing that results in the diagram not being displayed at all; the same effect for "Sankey2". "Sankey4" is displayed in grey regardless of the color scheme definition.

我还查看了由两者产生的html代码,产生"Sankey1"的R代码以及 christophergandrud.github.io/networkD3/#sankey .显然,该组之间存在差异:

I have also looked at the html code produced by both, my R code producing "Sankey1" as well as the code used on christophergandrud.github.io/networkD3/#sankey. Obviously, there is a difference regarding the group:

"Sankey1"中的HTML:

HTML from "Sankey1":

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

网络示例中的HTML摘录:

HTML excerpt from the web example:

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

更改"Sankey1"的输出html以反映Web示例的输出解决了此问题,"Sankey1"使用默认的颜色架构显示.

Changing the output html for "Sankey1" in order to reflect the output from the web example solves the issue, "Sankey1" is displayed using the default color schema.

此刻,我正在努力了解我所使用的数据的行为. sankey函数不依赖于强制列表输入;我实际上已经将网站上的示例数据集分成两个数据框(节点,链接),这确实产生了与Web示例相同的带有颜色的sankey图.因此,在我的示例中,输入数据肯定出了点问题……我想……任何帮助将不胜感激! 谢谢 奥利(Oli)

I am hitting a wall at the moment trying to understand the behavior for the data I am using. The sankey function does not rely on a mandatory list input; I have actually alos split the example data set from the website into two data frames (nodes, links), this does produce the same sankey diagram with colors as in the web example. Hence, something must be wrong with the input data in my example ... I guess ... Any help would be highly appreciated! Thanks Oli

推荐答案

我遇到了类似的问题.我通过减少节点总数(仅过滤高于特定值的边)来解决该问题.

I had a similar issue. I resolved it by decreasing the overall number of nodes (by filtering only edges above a certain value).

更多推荐

具有R库networkD3的Sankey图不显示颜色

本文发布于:2023-07-18 12:53:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   Sankey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!