5

I'm trying to create an annotated Sankey diagram. I'd like the final version to look along the lines of this manually annotated diagram:

enter image description here

The easy part of getting the Sankey diagram:

sankey_diagram <- plot_ly(
    type = "sankey",
    orientation = "h",

    node = list(
      label = c("Node_A_1", "Node_A_2", "Node_B_2", "Node_B_3", "Node_C_1", "Node_C_2"),
      color = c("blue", "blue", "blue", "blue", "blue", "blue"),
      pad = 15,
      thickness = 35,
      line = list(
        color = "black",
        width = 0.5
      )
    ),

    link = list(
      source = c(0,1,0,2,3,3),
      target = c(2,3,3,4,4,5),
      value =  c(8,4,2,8,4,2)
    )
  ) %>% 
  layout(
    font = list(
      size = 15
    )
)

At first I thought that if I want to get annotated "columns" I should turn to the annotations section of the plotly's documentation. The problem with annotations is that they are spatially limited to (at least I think so) the area of the graph. This is the code in the annotation-based approach:

# properties that hide the axes
ax <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE
)

sankey_diagram %>%
   add_annotations(
    x=-1,
    y=-5,
    xref = "x",
    yref = "y",
    text = "Column A",
    xanchor = 'right',
    showarrow = F
  ) %>% 
  add_annotations(
    x=0,
    y=-5,
    xref = "x",
    yref = "y",
    text = "Column B",
    xanchor = 'right',
    showarrow = F
  ) %>%
  add_annotations(
    x=1,
    y=-5,
    xref = "x",
    yref = "y",
    text = "Column C",
    xanchor = 'right',
    showarrow = F
  ) %>%  add_annotations(
    x=1,
    y=1,
    xref = "x",
    yref = "y",
    text = "", 
    xanchor = 'right',
    showarrow = F
  ) %>%
  layout(xaxis = ax, yaxis = ax)

The problem is that annotations land on the bottom of the graph but not under it.

The second approach is based on subplots. I create two subplots - the first one is Sankey, the other one is an empty one except for the annotations - and put them one in a row:

columns_plot <- plot_ly() %>% add_annotations(
    x=-1,
    y=-5,
    xref = "x",
    yref = "y",
    text = "Column A",
    xanchor = 'right',
    showarrow = F
  ) %>% 
  add_annotations(
    x=0,
    y=-5,
    xref = "x",
    yref = "y",
    text = "Column B",
    xanchor = 'right',
    showarrow = F
  ) %>%
  add_annotations(
    x=1,
    y=-5,
    xref = "x",
    yref = "y",
    text = "Column C",
    xanchor = 'right',
    showarrow = F
  ) %>%  add_annotations(
    x=1,
    y=1,
    xref = "x",
    yref = "y",
    text = "", 
    xanchor = 'right',
    showarrow = F
  ) %>%
  layout(xaxis = ax, yaxis = ax)

p <- subplot(sankey_diagram, columns_plot, nrows = 2, shareX = TRUE, margin = 0.1)
p %>%
  layout(xaxis = ax, yaxis = ax)

For some strange reason plotly puts the columns_plot on top of the sankey_diagram. I suspect that the second approach is the correct one but still I can't get the result depicted in the first paragraph of this question.

balkon16
  • 1,338
  • 4
  • 20
  • 40

0 Answers0