Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda