Sankey Diagram的自有颜色范围,带有NetworkD3包

AEd*_*ger 5 r d3.js sankey-diagram networkd3 htmlwidgets

我试图sankeyNetwork()在networkD3包中使用Sankey图.

sankeyNetwork(Links = Flow_data, Nodes = Nodes_data,
    Source = "Source_ID", Target = "Target",
    Value = "value", NodeID = "Nodes_name",
    width = 1000, height=600, fontsize = 16, nodeWidth = 50,
    colourScale = "d3.scale.category20c()")
Run Code Online (Sandbox Code Playgroud)

可视化效果很好,但我想将颜色范围更改为单个范围.有没有机会改变SankeyNetwork的颜色?我需要一系列只有3种颜色,我可以自己设置(不是d3.scale的预定义colourScales).

Kla*_*esi 6

你可以配置:

sankeyNetwork(Links = Flow_data, Nodes = Nodes_data,
                      Source = "Source_ID", Target = "Target",
                      Value = "value", NodeID = "Nodes_name",
                      width = 1000, height=600, fontsize = 16, nodeWidth = 50,
                      colourScale = "d3.scale.category20c()")  <==== Categorical color
Run Code Online (Sandbox Code Playgroud)

UPDATE

较新版本:

d3.scale.ordinal().range(["#7d3945","#e0677b", "#244457"])
now works if changed to:
d3.scaleOrdinal().range(["#7d3945","#e0677b", "#244457"]) 
Run Code Online (Sandbox Code Playgroud)

谢谢@Peter Ellis

UPDATE

使用自定义颜色时有没有办法设置透明度?

"#AARRGGBB"似乎不起作用

您可以创建一个selectAll("your_class").style("opacity",0.5),请看一下:stackoverflow.com/questions/6042550/...用于样式属性选项.CSS3有一个完全标准化的解决方案:"fill ="rgba(124,240,10,0.5)"

有关颜色参考,请查看此处:http://bl.ocks.org/aaizemberg/78bd3dade9593896a59d

在这里:https://github.com/mbostock/d3/wiki/Ordinal-Scales#categorical-colors

  • 根据文档:**colourScale字符串,指定节点**的分类颜色标度.您可以尝试这样:`"#7d3945","#e0677b","#244457"`或`d3.scale.ordinal().range(["#7d3945","#e0677b","#244457"]) `<==个人色彩 (3认同)