user15664585
user15664585

Reputation: 11

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.

I've updated R, and plotly, and everything else I can think of. 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. The effect of changing those parameters seems to be very unpredictable, and I can't suss out how they work.

Upvotes: 1

Views: 1191

Answers (1)

even_of_the_hour
even_of_the_hour

Reputation: 288

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. It seems that if any 0 values are present, the arguments are ignored with a silent error. 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

Upvotes: 1

Related Questions