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

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

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 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