0

I wish to use create a Sankey diagram in R with the sankeyNetwork() function from networkD3 where I want to control in what "columns" nodes are located. Consider the following code

library(networkD3)

links <- data.frame(source=c("1_a", "1_b", "1_c", "2_b", "2_d"),
                    target=c("2_a", "2_b", "2_c", "3_a", "3_b"),
                    value=c(3,2,5,4,7))

nodes <- data.frame(name=c("1_a", "1_b", "1_c", "2_b", "2_d", "2_a",
                            "2_c", "3_a", "3_b"))
                    
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")

This code produces the following plot:

plot1

However, I wish to align the nodes such that the first column contains "1_a", "1_b", "1_c", the second column "2_a", "2_b", "2_c" and "2_d" and the third "3_a" and "3_b". Adding sinksRight=FALSE does not help as it produces the following plot:

plot2

Is it possible to achieve the desired output?

matnor
  • 479
  • 1
  • 5
  • 17
  • You probably want [d3.sankeyJustify](https://github.com/d3/d3-sankey#sankeyJustify), but since networkD3 is using an older version of D3, it is not available through that package. – CJ Yetman Nov 12 '21 at 11:28
  • Ah, I see, thanks – matnor Nov 12 '21 at 11:36

0 Answers0