Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

573
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!