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
instead of following which I'm seeing now
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)
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)