Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda