0

I have color added to my Sankey diagram as per this link. But I want to specify the color between nodes based on another column. i.e. my node/edge list looks like the following.

Node1  | Node 2  |  Weight  | Color
N1     | N2      |  8       | #fff000
N2     | N3      | 10       | #hhh123

How would I edit the d3.style part of the R file to take in this extra column value and use it as the color between nodes?

Here's the full R file, the d3 part that needs to be fixed is halfway down

require(rCharts)
require(igraph)

g2 <- graph.tree(40, children=4)

gallery2 <- read.csv('C:/Users/Rob/Dropbox/BP_Rob_Folder/Topic_Models/nodes_colors.csv', stringsAsFactors = FALSE)

edgelistWeight <- gallery2
colnames(edgelistWeight) <- c("source","target","value","color")
edgelistWeight$source <- as.character(edgelistWeight$source)
edgelistWeight$target <- as.character(edgelistWeight$target)
edgelistWeight$color <- as.character(edgelistWeight$color)
sankeyPlot2 <- rCharts$new()
sankeyPlot2$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey')
sankeyPlot2$setTemplate(script = 'http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey/layouts/chart.html')
sankeyPlot2$set(
  data = edgelistWeight,
  nodeWidth = 15,
  nodePadding = 10,
  layout = 32,
  width = 960,
  height = 500
)

### How do I use the weight list in this afterscript part?###

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

sankeyPlot2
Community
  • 1
  • 1
SharpObject
  • 597
  • 5
  • 17

0 Answers0