【问题标题】:plotly Sankey diagram: How to change default order of nodesplotly Sankey 图:如何更改节点的默认顺序
【发布时间】:2018-07-28 02:34:36
【问题描述】:

我使用plotly 包创建了一个桑基图。

据我所知,节点的默认顺序主要由值定义。但是,我希望不使用鼠标药物手动移动节点的字母顺序。

我可以用 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))

【问题讨论】:

  • 您找到解决方案了吗?如果没有,您可以考虑提供赏金以给予更多关注。
  • 目前看来不可能:github.com/plotly/plotly.py/issues/960
  • @Wolfgang;哦,好吧,那太糟糕了。感谢您的评论。
  • 现在你可以通过.node(x|y)github.com/plotly/plotly.js/pull/3583定义位置
  • @banderlog013 ;一百万谢谢!!!!我会发布你提到的答案。

标签: r plotly sankey-diagram


【解决方案1】:

这可能值得检查,但我发现它对我的情况很有帮助(图表有点复杂)。 如果在组织数据标签时,请确保按照您希望显示的顺序组织列表。

例如,如果有一个节点需要提前进入(左),只需在其余节点之前弹出它 labelList.insert(0, labelList.pop(labelList.index(first_node_label)))

然后将labelList 插入go,您应该对结构有一些(尽管不是绝对的)控制权。

【讨论】:

  • 感谢您的回答。你的代码是 Python,不是吗?我将尝试在 R 中查看选项。
  • 是的,抱歉应该提到。
【解决方案2】:

在@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))

并且请给一个节点下药以反映节点的位置。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多