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

575
Vistas
How to force networkD3::sankeyNetwork() to repeat nodes rather than circling back to same node

Hi I am creating a sankey diagram to show migration. One aspect I am facing issues with is how to deal with scenarios when status doesn't change i.e. migration doesn't happen. In such scenarios, the sankey is circling back to same source, creating a circle rather than using a 'source- target' layout where both source destination is separate. I'd like to see something like

enter image description here

instead of following which I'm seeing now enter image description here

Following is a reproducible example- I appreciate any assistance with this

library(networkD3)
library(dplyr)
links<-data.frame(
  source=c('One','Two','Two','Three','Three','Four','Four','Five','Five','Six'),
  target=c('Two','Two','Three','Three','Four','Four','Five','Five','Six','Seven'),
  value=c(78,108,27,14,31,38,17,76,10,89)
)
nodes <- data.frame(
  name=c(as.character(links$source), 
         as.character(links$target)) %>% unique()
)
links$IDsource <- match(links$source, nodes$name)-1 
links$IDtarget <- match(links$target, nodes$name)-1

sankeyNetwork(Links = links, Nodes = nodes,
              Source = "IDsource", Target = "IDtarget",
              Value = "value", NodeID = "name", 
              fontSize = 20)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Every node you want to see in the plot has to be uniquely identified in your nodes data.frame. If you want some unique nodes to share the same label, you can add a column to the nodes data.frame for the label and specify it to the NodeID argument... those do not need to be unique.

library(networkD3)

links <- read.csv(header = TRUE, strip.white = TRUE, text ="
source,    target,    value
One_1,     Two_2,     78
Two_2,     Two_3,     108
Two_2,     Three_3,   27
Three_3,   Three_4,   14
Three_3,   Four_4,    31
Four_4,    Four_5,    38
Four_4,    Five_5,    17
Five_5,    Five_6,    76
Five_5,    Six_6,     10
Six_6,     Seven_7,   89
")

id <- unique(c(as.character(links$source), as.character(links$target)))
label <- sub("_[0-9]", "", id)
nodes <- data.frame(id = id, label = label)

links$IDsource <- match(links$source, nodes$id)-1 
links$IDtarget <- match(links$target, nodes$id)-1

sankeyNetwork(Links = links, Nodes = nodes,
              Source = "IDsource", Target = "IDtarget",
              Value = "value", NodeID = "label", 
              fontSize = 20)

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