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

203
Vistas
how to make sankyplots with no color for source and target node and how to do legend to show the different colors of linkgroup

I am working on network data and want to visualize it using sankeyNetwork() from the networkd3 package in r. In my data frame, I have a source node and a target node, value, and a grouping column group.

  1. How can I make the sankey plot without coloring the nodes (both source and target with no color)?
  2. I want to color the link lines by a variable group LinkGroup = "group" and I want to know how to make a legend to show that the different colors of the links are actually representing the group variable.

here is the code and the data frame...

# Libraries
library(dplyr)
library(tidyr)
library(tibble)
library(networkD3)

# Load dataset from github

data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyDirectedWeighted.csv", header=TRUE) %>%
  rownames_to_column() %>%
  gather(key = "key", value = "value", -rowname) %>%
  filter(value > 0) %>% 
  rename(source = rowname,  target =key) %>%
  select("source", "target", "value") %>%
  mutate(group = c(rep("A", 10), rep("B",7), rep("C", 8), rep("D", 10)))
nodes <- data.frame(name=c(as.character(data$source), as.character(data$target)) %>% unique())

data$IDsource=match(data$source, nodes$name)-1 
data$IDtarget=match(data$target, nodes$name)-1

# Make the Network
sankeyNetwork(Links = data,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value",
              NodeID = "name", 
              sinksRight=FALSE,
              nodeWidth=40,
              fontSize=13,
              nodePadding=20,
              LinkGroup = "group")
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can continue to let the link nodes be given automatically chosen colors based on their group, and effectively turn off coloring of the nodes with the NodeGroup = NULL option/argument

sankeyNetwork(Links = data,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value",
              NodeID = "name", 
              sinksRight=FALSE,
              nodeWidth=40,
              fontSize=13,
              nodePadding=20,
              LinkGroup = 'group',
              NodeGroup = NULL)

enter image description here


networkD3 does not have any built-in legend creation options, however, if you want to make a legend yourself, you'll probably want to use a consistent set of colors so that you can use them with whatever you use to make a legend. You can do that like this...

colourScale <- 
  'd3.scaleOrdinal()
     .domain(["A", "B", "C", "D"])
     .range(["red", "blue", "green", "yellow"])'

sankeyNetwork(Links = data,
              Nodes = nodes,
              Source = "IDsource",
              Target = "IDtarget",
              Value = "value",
              NodeID = "name", 
              sinksRight=FALSE,
              nodeWidth=40,
              fontSize=13,
              nodePadding=20,
              LinkGroup = 'group',
              NodeGroup = NULL,
              colourScale = colourScale)

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