1

I'd like to create a Sankey plot with plotly. My data is structured in that way, that there is flow from the very left to the very right of plot. Each flow has the same value.

The problem is: Currently it is not possible for me to rearrange all the flows in the plot so that each link between the nodes is connected with its previous or following nodes. This makes a flow not continuous as the flow is sometimes "cut" and continous at a diffrent position again.

I'd like have a plot where each flow, from its first node to its last node, is aligned so that you can follow this flow easily.

I already tried to rearrange the nodes manually. This led to an improvement but it does not solve the problem entirely.

This is a minimal example of my code

library(plotly)

p <-plot_ly(
  type='sankey',
  alpha = 0.1,
  node = list(
    pad = 15,
    thickness = 20,
    line = list(
      color = "black",
      width = 0.5
    ),
    label = c("A1", "A2", "B1", "C1","D3" ,"D2", "D1"),
    color = 'rgba(255,255,255,0.8)'
  ),
  link = list(
    source = c(0,1,0,2,2,2,3,3,3),
    target = c(2,2,2,3,3,3,4,5,6),
    value = c(8,4,1,1,8,4,1,4,8),
    color = c('rgba(189,10,10,0.3)','rgba(0,0,0,0.3)', 'rgba(3,189,5,0.3)',
              'rgba(3,189,5,0.3)','rgba(189,10,10,0.3)', 'rgba(0,0,0,0.3)',
              'rgba(3,189,5,0.3)', 'rgba(0,0,0,0.3)', 'rgba(189,10,10,0.3)'
    )
  )
)

p

This code produces this plot: sankey_plot

When I rearrange the node manually, I get this: sankey_plot_rearranged

This close to what I want, but is there option in plotly to achieve this without rearranging the node manually? Maybe there is also another programmtic approach anyone likes to share?

Thanks!

0 Answers0