plotly / plotly.R

An interactive graphing library for R

Home Page:https://plotly-r.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sankey diagram: position of node cannot be rendered correctly

JinTonique opened this issue · comments

Hello people!
I am trying to make a sankey diagram with Plotly, however, the alignment of certain node cannot be rendered correctly as I set.
PS: my Plotly version is 4.10.3 and R version is 4.3.1

Here is my code:

library("plotly")

# My data base
values <- c(10,1811,530,363,271,601,681,37,46,18,109,39,3,22,74,22,10,
            14,3,81,0,1,19,1807,521,363,270,601,16,36,8,46,86,1,0,0,23,
            14,18,34,684,23,0,16,0,23,0,0,0,23,14,18,69,1,34,0,0,0,684,
            43,643,101,6,68,43,620,101,6,19,1807,521,363,270,601,16,36,
            8,46,15,1555,79,244,346,4,115,2,2,0,4,17,251,265,1,3,25,3,
            16,0,8,43,105,24,2,1,1,6,0,601,0,129,196,109,14,82,124,1,
            34,2,0,13,0,11)
target <- c(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,4,
            4,4,5,5,5,5,5,5,5,5,5,5,5,10,10,10,10,10,10,10,10,11,11,11,
            11,11,11,11,11,6,6,6,6,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,13,13,13,
            13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,15,15,
            15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,16,9,9,9,9)
source <- c(0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,
            2,2,2,2,2,2,2,2,2,2,2,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,
            11,11,11,6,6,6,6,6,4,4,4,4,4,4,4,4,4,4,8,8,8,8,8,8,8,8,8,8,
            8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8)

num_node <- c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
x_position <- c(0.05,0.05,0.2,0.35,0.35,0.35,0.65,0.8,0.8,0.9,0.45,0.45,0.45,0.999,0.999,0.999,0.999)
y_position <- c(0.45,0.6,0.55,0.15,0.45,0.6,0.75,0.85,0.55,0.99,0.6,0.6,0.6,0.3,0.3,0.3,0.3)

# Sankey diagram with Plotly
fig <- plot_ly(
  type = "sankey",
  arrangement = "snap",
  node = list(
    label = num_node,
    x = x_position,
    y = y_position,
    pad = 15,
    thickness = 15
  ),
  link = list(
    source = source,
    target = target,
    value =  values
  )
)

fig

What I would expect is that node 13, 14, 15, 16 be at the same colomn, as I set their x position to 0.999. See picture below:
Capture d’écran 2024-01-11 à 18 38 34

However, for some reason, my node 13 would like to be in the middle of my diagram instead of the position where it should have been. See picture below:
Capture d’écran 2024-01-11 à 18 41 16

Does anyone have some insights why the node cannot accept x and y position I set? Thank you guys very much!