1

I've created a large animated sankey diagram with Plotly in R. At each step in the animation Plotly rescales the the height of the links, whereas the height should be constant over time. That is, a link with a constant value should always have the same height and should not be rescaled. In the example below the link between A2 and B2 should remain the same height, rather than be rescaled so that the links fill 100% of the canvas height. https://plotly.com/r/reference/#sankey mentions ids as an option for object constancy of data points during animation. I'm not sure whether it will do the trick, as I haven't figured out how to implement it. This or any other way to achieve constant scale would be extremely helpful. enter image description here The result should look more or less like the gif. I've "cheated" here by creating dummy nodes that take the space that should otherwise be empty. This method is less feasible for the end diagram, which is more complex than the canonical energy forecast Sankey by Mike Bostock

library(plotly)

S.links <- data.frame(source = c(0,1,0,2,3,3, 0,1,0,2,3,3),
                    target = c(2,3,3,4,4,5, 2,3,3,4,4,5),
                    value =  c(8,4,2,8,4,2, 16,4,2,16,4,2),
                    year = c("2001","2001","2001","2001","2001","2001","2002","2002","2002","2002","2002","2002"))

plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = c("A1", "A2", "B1", "B2", "C1", "C2"),
    color = c("blue", "blue", "blue", "blue", "blue", "blue"),
    pad = 15,
    thickness = 20,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = S.links,
  frame = ~S.links$year
)
Julian
  • 11
  • 3

1 Answers1

0

I guess you need the group argument

library(plotly)

S.links <- data.frame(source = c(0,1,0,2,3,3, 0,1,0,2,3,3),
                      target = c(2,3,3,4,4,5, 2,3,3,4,4,5),
                      value =  c(8,4,2,8,4,2, 16,4,2,16,4,2),
                      year = c("2001","2001","2001","2001","2001","2001",
                               "2002","2002","2002","2002","2002","2002"))
S.links
#>    source target value year
#> 1       0      2     8 2001
#> 2       1      3     4 2001
#> 3       0      3     2 2001
#> 4       2      4     8 2001
#> 5       3      4     4 2001
#> 6       3      5     2 2001
#> 7       0      2    16 2002
#> 8       1      3     4 2002
#> 9       0      3     2 2002
#> 10      2      4    16 2002
#> 11      3      4     4 2002
#> 12      3      5     2 2002

plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = c("A1", "A2", "B1", "B2", "C1", "C2"),
    color = c("blue", "blue", "blue", "blue", "blue", "blue"),
    pad = 15,
    thickness = 20, 
    # added the group argument
    group = 1,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = S.links,
  frame = ~S.links$year
)

sankeygif

mnist
  • 6,571
  • 1
  • 18
  • 41
  • I have tried this and it does not seem to work. I checked the reference guide and node only has a groups attribute, which I tried as well. In both cases it still rescaled the height of node A2, which has the same value. Is there a package you need to have installed or did I miss something else? – Julian Sep 08 '21 at 18:05
  • I included a gif of how it looks like. what is wrong here? maybe I missed your point – mnist Sep 08 '21 at 20:20