简体   繁体   English

桑基图中 R 中的 Plotly 节点放置

[英]Plotly node placement in R in a Sankey Diagram

Okay, I can't figure out what I'm doing wrong here.好吧,我不知道我在这里做错了什么。

I want the nodes to be positioned green, yellow, red in descending order.我希望节点按降序排列为绿色、黄色、红色。 I'm trying to create a number of them, so I don't want to have to position the nodes by hand in Viewer.我正在尝试创建其中的一些,所以我不想在查看器中手动 position 节点。

I've updated R, and plotly, and everything else I can think of.我已经更新了 R 和 plotly,以及我能想到的所有其他内容。 Through trial and error I think I have the right side in the correct order, but the left side still bedevils me.通过反复试验,我认为我的右侧顺序正确,但左侧仍然困扰着我。

fig <- plot_ly(type = 'sankey',
               orientation = 'h',
               arrangement = 'snap',
               node = list(label = c("Low", "Moderate", "High", "-4.9%", "+0%", "+4.9%"),
                           color = c('green', 'yellow', 'red', 'green', 'yellow', 'red'),
                           y = c(0, .1, .5, 0, .1, .5),
                           x = c(0, 0, 0, 1, 1, 1),
                           pad = 10,
                           thickness = 20,
                           line = list(color = 'black',
                                       width = .5)
                           ),
               link = list(source = c(0, 0, 0, 1, 1, 1, 2, 2, 2),
                           target = c(3, 4, 5, 3, 4, 5, 3, 4, 5),
                           value =  c(17,7, 8, 5, 1,10, 5, 8,42)))

fig <- fig %>%
  layout()

fig

Edit: To be more specific about my question, I don't understand how the x and y coordinates work.编辑:为了更具体地说明我的问题,我不明白 x 和 y 坐标是如何工作的。 The effect of changing those parameters seems to be very unpredictable, and I can't suss out how they work.更改这些参数的效果似乎非常不可预测,我无法弄清楚它们是如何工作的。

According to this open issue , node.x and node.y arguments for manual positions can't be equal to 0. Changing the 0 values to 0.001 in your code fixes the ordering.根据这个未解决的问题,手动位置的 node.x 和 node.y arguments 不能等于 0。将代码中的 0 值更改为 0.001 可以修复排序。 It seems that if any 0 values are present, the arguments are ignored with a silent error.似乎如果存在任何 0 值,则 arguments 将被忽略并出现无提示错误。 I have been digging into this recently and opened a related issue about the documentation and general problems with overriding the node order.我最近一直在研究这个问题,并打开了一个关于文档和覆盖节点顺序的一般问题的相关问题

fig <- plot_ly(type = 'sankey',
               orientation = 'h',
               arrangement = 'snap',
               node = list(label = c("Low", "Moderate", "High", "-4.9%", "+0%", "+4.9%"),
                           color = c('green', 'yellow', 'red', 'green', 'yellow', 'red'),
                           y = c(0.001, .1, .5, 0, .1, .5),
                           x = c(0.001, 0.001, 0.001, 1, 1, 1),
                           pad = 10,
                           thickness = 20,
                           line = list(color = 'black',
                                       width = .5)
               ),
               link = list(source = c(0, 0, 0, 1, 1, 1, 2, 2, 2),
                           target = c(3, 4, 5, 3, 4, 5, 3, 4, 5),
                           value =  c(17,7, 8, 5, 1,10, 5, 8,42)))

fig <- fig %>%
  layout()

fig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM