R中带有networkD3包的Sankey图的自有颜色范围 [英] Own colour range for Sankey Diagram with networkD3 package in R
问题描述
我正在尝试使用networkD3软件包中的sankeyNetwork()
绘制Sankey图.
I am trying to plot Sankey diagrams using sankeyNetwork()
in networkD3 package.
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()")
可视化效果很好,但我想将颜色范围更改为单个范围.有没有机会改变SankeyNetwork的颜色?我只需要一个范围我可以自行设置3种颜色(不是d3.scale的预定义colourScales).
The visualization works great but I would like to change the colour range to an individual range. Is there any chance to change the colours of the SankeyNetwork? I need a range of only e.g. 3 colours which I can set by myself (not the predefined colourScales of d3.scale).
推荐答案
您可以配置:
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
更新
较新版本:
d3.scale.ordinal().range(["#7d3945","#e0677b", "#244457"])
now works if changed to:
d3.scaleOrdinal().range(["#7d3945","#e0677b", "#244457"])
感谢@Peter Ellis
Thanks @Peter Ellis
更新
使用自定义颜色时是否可以设置透明度?
Is there any way to set transparency when using custom colours?
#AARRGGBB"似乎不起作用
"#AARRGGBB" doesn't seem to work
您可以创建一个selectAll("your_class").style("opacity",0.5),请看一下:
You can make a selectAll("your_class").style("opacity",0.5), Take a look to this: stackoverflow.com/questions/6042550/… for style attribute options. And CSS3 has a fully standardized solution: "fill="rgba(124,240,10,0.5)"
有关颜色参考,请参见此处: http://bl.ocks.org/aaizemberg/78bd3dade9593896a59d
For color references, look here: http://bl.ocks.org/aaizemberg/78bd3dade9593896a59d
和此处: https://github.com/mbostock/d3/wiki/Ordinal-Scales#categorical-colors
这篇关于R中带有networkD3包的Sankey图的自有颜色范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!