Own colour range for Sankey Diagram with networkD3 package in R
Asked Answered
S

1

5

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

Shih answered 25/8, 2015 at 16:32 Comment(0)
T
8

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

Theophilus answered 25/8, 2015 at 16:42 Comment(8)
Thank you :) I visited these links already. But with the categorical colours I get 10 or 20 predefined colours. I would prefer to use less colours. Is there a possibility to pick only a few of the categorical colours?Shih
According documentation: colourScale character string specifying the categorical colour scale for the nodes. You can try this: "#7d3945","#e0677b", "#244457" or d3.scale.ordinal().range(["#7d3945","#e0677b", "#244457"]) <== personal colorsTheophilus
Hi Klaujesi, do you mind taking a look at a related networkD3 question? #35280718Term
Is there any way to set transparency when using custom colours? #AARRGGBB doesn't seem to workAlkalinity
You can make a selectAll("your_class").style("opacity",0.5), Take a look to this: #6043050 for style attribute options. And CSS3 has a fully standardized solution: "fill="rgba(124,240,10,0.5)"Theophilus
Should this answer still work? Doesn't work for me any more, wondering if something has changed. When I add colourScale = "d3.scale.category20c()" to sankeyNetwork it no longer draws the diagram.Sensuous
d3.scale.ordinal().range(["#7d3945","#e0677b", "#244457"]) now works if changed to d3.scaleOrdinal().range(["#7d3945","#e0677b", "#244457"])Sensuous
if change to image?Outtalk

© 2022 - 2024 — McMap. All rights reserved.