Sankey Charting: How to Reorder Default Nodes

I created a Sankey diagram using the package plotly.

As far as I can see, the order of the default nodes is mainly determined by the value. However, I want an alphabetical order without manually moving nodes with a mouse preparation.

Can I change the default order with R?

Any help would be greatly appreciated. The following is sample code and results:

node_label <- c("x1_1", "x1_2", "x2_1", "x2_2")
link_source <- c(0, 0, 1, 1)
link_target <- c(2, 3, 2, 3)
link_value <- c(2, 5, 1, 3)
# when link_value <- c(5, 2, 1, 3), the order is changed.

plotly::plot_ly(
  type = "sankey", 
  domain = list(x =  c(0,1), y =  c(0,1)), 
  node = list(label = node_label),
  link = list(
    source =  link_source,
    target = link_target,
    value =  link_value))

enter image description hereenter image description here

+12
source share
1 answer

this might be worth checking out, but I found it useful in my case (where the diagram was a bit more complicated). If you are organizing data labels, make sure that the list is ordered in the order in which you want to display it.

, , (), labelList.insert(0, labelList.pop(labelList.index(first_node_label)))

labelList go, ( ) .

+1

Source: https://habr.com/ru/post/1693765/


All Articles