plotly Sankey图:如何更改节点的默认顺序

cut*_*h44 14 r plotly sankey-diagram

我使用plotly包创建了一个Sankey图.

据我所知,节点的默认顺序主要由值定义.但是,我想要字母顺序,而无需手动移动鼠标药物的节点.

我可以用R更改默认顺序吗?

任何帮助将不胜感激.下面是一个示例代码和输出:

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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述在此输入图像描述

cut*_*h44 1

根据@banderlog013的建议,我实现了手动定义节点的顺序。
十分感谢 !!!
以下是我的示例答案。

node_label <- c("x1_1", "x1_2", "x2_1", "x2_2")
node_x <- c(0, 0, 1, 1)
node_y <- c(0, 1, 0, 1)
link_source <- c(0, 0, 1, 1)
link_target <- c(2, 3, 2, 3)
link_value <- c(2, 5, 1, 3)

plotly::plot_ly(
  type = "sankey", 
  arrangement = "snap",
  node = list(
    label = node_label,
    x = node_x,
    y = node_y,
    pad = 20),  
  link = list(
    source =  link_source,
    target = link_target,
    value =  link_value))
Run Code Online (Sandbox Code Playgroud)

请对节点进行一点药物以反映节点位置。

在此输入图像描述

  • 请注意,存在一些问题:https://github.com/plotly/plotly.py/issues/1732、https://github.com/plotly/plotly.py/issues/3002、https:// github.com/plotly/plotly.py/issues/3003 不确定它们是否存在于 R 版本中 (3认同)