Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Override default networkD3 graph properties

I am trying to set a custom color scheme using sankeyNetwork() from the networkd3 package in r. This is the custom color code I set for the colourScale argument of sankeyNetwork()...

# Give a color for each group:
my_color <- 
  'd3.scaleOrdinal()
     .domain([
       "Piped Water", 
       "Dug well protected",
       "Tube well, borehole", 
       "Spring protected",
       "Rainwater",
       "Dug well unprotected",
       "Spring unprotected",
       "Tanker truck / cart",
       "Surface water",
       "Other",
       "node_color"
     ])
     .range([
       "#2D20E1",
       "#ABA7ED",
       "#8726BE",
       "#12B3F3",
       "#6178e8",
       "#F6F614",
       "#BDBD07",
       "#F4710E",
       "#EEB56E",
       "#6F3609"
     ])'

However the custom colors set in this code do not appear as I expect in the output sankey plot.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's possible you're running into a problem with D3/JavaScript comparing only the first word of your LinkGroup values, i.e. ignoring everything after the first space. You can work around that by replacing the spaces with something else, like a "_", in both the Links data frame and the JavaScript in my_color.

Here's a full reproducible example...

library(networkD3)

nodes <- read.table(header = TRUE, text = '
  name       node_group
  "Node 0"   "node_color"
  "Node 1"   "node_color"
  "Node 2"   "node_color"
  "Node 3"   "node_color"
  "Node 4"   "node_color"
  "Node 5"   "node_color"
  "Node 6"   "node_color"
  "Node 7"   "node_color"
  "Node 8"   "node_color"
  "Node 9"   "node_color"
  "Node 10"  "node_color"
')

links <- read.table(header = TRUE, text = '
  source target value link_group
  0      10     1     "Piped Water"
  1      10     1     "Dug well protected"
  2      10     1     "Tube well, borehole"
  3      10     1     "Spring protected"
  4      10     1     "Rainwater"
  5      10     1     "Dug well unprotected"
  6      10     1     "Spring unprotected"
  7      10     1     "Tanker truck / cart"
  8      10     1     "Surface water"
  9      10     1     "Other"
')

links$link_group <- gsub(" ", "_", links$link_group)

my_color <- paste0(
  'd3.scaleOrdinal()
     .domain(["', 
     paste0(links$link_group, collapse = '", "'),
     '"])
     .range([
       "#F00", // red
       "#0F0", // green
       "#00F", // blue
       "#FF0", // yellow
       "#71A", // purple
       "#EEE", // off-white
       "#666", // grey
       "#000", // black
       "#520", // brown
       "#FA0", // orange
       "#AAA"
     ])')

sankeyNetwork(
  Links = links,
  Nodes = nodes,
  Source = "source",
  Target = "target",
  Value = "value",
  NodeID = "name",
  NodeGroup = "node_group",
  LinkGroup = "link_group",
  colourScale = my_color
)

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!