Adding color to a Sankey chart in rCharts

I created a sankey diagram in rCharts, but I have one question. How to add color? I would like to present each node with a different color so that it is easier to visualize the paths, instead of just seeing the same gray lines connecting everything. Code and output below:

require(rCharts) require(rjson) x = read.csv('/Users/<username>/sankey.csv', header=FALSE) colnames(x) <- c("source", "target", "value") sankeyPlot <- rCharts$new() sankeyPlot$set( data = x, nodeWidth = 15, nodePadding = 10, layout = 32, width = 500, height = 300, units = "TWh", title = "Sankey Diagram" ) sankeyPlot$setLib('http://timelyportfolio.imtqy.com/rCharts_d3_sankey') sankeyPlot 

Here my chart looks like

enter image description here

Many thanks!

+7
r sankey-diagram rcharts
source share
1 answer

not sure which colors you want, but if you installed a new rCharts with devtools::install_github("ramnathv/rCharts") , here is how you can color based on the original value with a demo here .

 require(rCharts) require(rjson) x = read.csv('/Users/<username>/sankey.csv', header=FALSE) colnames(x) <- c("source", "target", "value") sankeyPlot <- rCharts$new() sankeyPlot$set( data = x, nodeWidth = 15, nodePadding = 10, layout = 32, width = 500, height = 300, units = "TWh", title = "Sankey Diagram" ) sankeyPlot$setLib('http://timelyportfolio.imtqy.com/rCharts_d3_sankey') sankeyPlot$setTemplate( afterScript = " <script> // to be specific in case you have more than one chart d3.selectAll('#{{ chartId }} svg path.link') .style('stroke', function(d){ //here we will use the source color //if you want target then sub target for source //or if you want something other than gray //supply a constant //or use a categorical scale or gradient return d.source.color; }) //note no changes were made to opacity //to do uncomment below but will affect mouseover //so will need to define mouseover and mouseout //happy to show how to do this also // .style('stroke-opacity', .7) </script> ") sankeyPlot 

If you want to use d3.scale.category??() to provide your color, I assume that you will want to color the node rectangle as well. Here is one example of a color change for both node and link.

 sankeyPlot$setTemplate( afterScript = " <script> var cscale = d3.scale.category20b(); // to be specific in case you have more than one chart d3.selectAll('#{{ chartId }} svg path.link') .style('stroke', function(d){ //here we will use the source color //if you want target then sub target for source //or if you want something other than gray //supply a constant //or use a categorical scale or gradient //return d.source.color; return cscale(d.source.name); }) //note no changes were made to opacity //to do uncomment below but will affect mouseover //so will need to define mouseover and mouseout //happy to show how to do this also // .style('stroke-opacity', .7) d3.selectAll('#{{ chartId }} svg .node rect') .style('fill', function(d){ return cscale(d.name) }) .style('stroke', 'none') </script> ") sankeyPlot 
+10
source share

All Articles