1

Is it possible to make a sankey plot that looks like this in R? Example of required functionality

I've played around with the networkD3 package, which works great if the sum of input = sum of output for each node, but I need a solution which supports bands/links that change in size between time periods.

As far as I can work out, networkD3 does not allow me to do this because you must specify the links with a single value (links have a fixed width/value).

Is there anything out there, preferably for R, that can do this?

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
dcl
  • 929
  • 1
  • 10
  • 22

2 Answers2

3
library(networkD3)

links <- read.csv(header = T, text = "
source,target,value
0,1,50
0,2,50
1,3,50
2,4,25
3,5,75
4,5,25
")

nodes <- data.frame(name = c("A", "A", "B", "A", "B", "A"))

sankeyNetwork(links, nodes, "source", "target", "value", "name")

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • 1
    I think the issue is A -> A in the middle should go from 50 to 75, so there is no empty space on target A. – zx8754 Jan 10 '18 at 09:20
  • 2
    Links have a fixed value/width. – CJ Yetman Jan 10 '18 at 09:21
  • 2
    I understand, just trying to clarify OP's point. Thank you for the package! :D – zx8754 Jan 10 '18 at 09:23
  • Thanks @CJYetman. That is helpful, but I think now my simple example was not specific enough. Would something like this be possible? https://i.imgur.com/GUw3KF9.png . Because we can't specify node sizes, it seems as though the sum of the value of the T2 nodes, is going to be equal to the sum of the value of the T1 nodes (except if the outflow from T2 is larger than the outflow from T1)? – dcl Jan 10 '18 at 22:43
  • 1
    Sankey diagrams are flow diagrams, so it’s all about the links/edges/flows. The nodes don’t have values, the links do. The nodes simply adapt to whatever the size of the links running into them. – CJ Yetman Jan 10 '18 at 23:01
  • @CJYetman, Understood. Thank you. Looks like I'll need to find a different solution. – dcl Jan 11 '18 at 00:13
1

A possible solution to this problem is to use an alluvial plot from the ggalluvial package.

Source: Weighted sankey / alluvial diagram for visualizing discrete and continuous panel data?

dcl
  • 929
  • 1
  • 10
  • 22