Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Own colour range for Sankey Diagram with networkD3 package in R

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()")

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).

like image 649
AEdiger Avatar asked Aug 25 '15 16:08

AEdiger


Video Answer


1 Answers

You can config:

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

UPDATE

Newer version:

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

Thanks @Peter Ellis

UPDATE

Is there any way to set transparency when using custom colours?

"#AARRGGBB" doesn't seem to work

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)"

For color references, look here: http://bl.ocks.org/aaizemberg/78bd3dade9593896a59d

and here: https://github.com/mbostock/d3/wiki/Ordinal-Scales#categorical-colors

like image 92
Klaujesi Avatar answered Nov 14 '22 22:11

Klaujesi