networkD3 sankeyGraph() 的自定义颜色

Custom Colors for networkD3 sankeyGraph()

Important clarification: I am using an older, non-CRAN branch of networkD3 forked by Florian Breitwieser fbreitwieser/networkD3/tree/add_zoom. Florian's branch added zooming capabilities to Sankey graphs, but was not merged into the CRAN package. Since that particular branch has not been updated since 2016, the syntax here differs than what should be used with the current (as of 2018-02-15) CRAN release.


我正在尝试将自定义颜色应用于 networkD3 sankeyGraph()

我正在尝试重现 https://www.r-graph-gallery.com/322-custom-colours-in-sankey-diagram/ 中的示例 #1。

我猜自从该示例最初发布以来可能已经进行了一两次软件包更新,关于此处可能需要更改的内容有什么想法吗?

(几乎一字不差)示例代码

(我所做的唯一非格式更改是直接加载 magrittr 而不是为管道堆放十几个 tidyverse 包)

# Library
library(networkD3)
#library(tidyverse)
library(magrittr)

# Make a connection data frame
links=data.frame(source=c("group_A","group_A", "group_B", "group_C", "group_C", "group_E"),
                 target=c("group_C","group_D", "group_E", "group_F", "group_G", "group_H"),
                 value=c(2,3, 2, 3, 1, 3))

# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes=data.frame(name=c(as.character(links$source), as.character(links$target)) %>% unique())
links$IDsource=match(links$source, nodes$name)-1 
links$IDtarget=match(links$target, nodes$name)-1

# prepare color scale: I give one specific color for each node.
my_color <- 'd3.scaleOrdinal() .domain(["group_A", "group_B","group_C", "group_D", "group_E", "group_F", "group_G", "group_H"]) .range(["blue", "blue" , "blue", "red", "red", "yellow", "purple", "purple"])'

# Make the Network. I call my colour scale with the colourScale argument
sankeyNetwork(Links = links,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value",
              colourScale= my_color,
              NodeID = "name")

应该得出什么:

我得到的结果:

注释掉 colourScale 表明其他一切正常。

sankeyNetwork(Links = links,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value",
              # colourScale= my_color,
              NodeID = "name")

我试过的一些东西:

问答 看起来很有希望,但是当我 运行 标记为已接受答案时,我再次得到空白输出。

How to color groups in networkD3's sankeyNetwork? 似乎也很有前途,因为它有一个包贡献者 (@cjyetman) 的答案,虽然我能够重现他的最小示例,但我没有成功添加自定义颜色映射。

Own colour range for Sankey Diagram with networkD3 package in R 似乎又一次涵盖了适当的语法,但我再次将那里的建议应用到这个问题上。

Important clarification: I am using an older, non-CRAN branch of networkD3 forked by Florian Breitwieser fbreitwieser/networkD3/tree/add_zoom. Florian's branch added zooming capabilities to Sankey graphs, but was not merged into the CRAN package. Since that particular branch has not been updated since 2016, the syntax here differs than what should be used with the current (as of 2018-02-15) CRAN release.

哎呀,终于根据

搞定了

此用法的正确格式如下。

my_color <- 'd3.scale.category10().range(["blue", "blue" , "blue", "red", "red", "yellow", "purple", "purple"]).domain(["group_A", "group_B","group_C", "group_D", "group_E", "group_F", "group_G", "group_H"])'

另外一个问题我终于想通了:domain中的组名不能有空格!

这可行:

'd3.scale.category10().range(["blue", "red"]).domain(["group_A", "group_B"])'

这不会!

'd3.scale.category10().range(["blue", "red"]).domain(["group A", "group B"])'