Cannot plot Sankey Diagram

Hi there,
I’m trying to plot a sankey diagram, but it’s generated blank.
Following is my code:

p <- plot_ly(
    type = "sankey",
    domain = c(
      x = c(0, 1),
      y = c(0, 1)
    ),
    orientation = "h",

    node = list(
      label = c("Alarm 103", "Alarm 23",  "Alarm 30",  "Alarm 71",  "Alarm 91",  "Model 0M",  "Model MA",  "Model MC",  "Model ML",  "Model MX",  "Model S3",  "Model S7",  "Serie 0",   "Serie 2",   "Serie 3",   "Serie 4",   "Serie 5"  ),
      color = c("#377EB860", "#377EB860", "#377EB860", "#377EB860", "#377EB860", "#E41A1C60", "#E41A1C60", "#E41A1C60", "#E41A1C60", "#E41A1C60", "#E41A1C60", "#E41A1C60", "#4DAF4A60", "#4DAF4A60", "#4DAF4A60", "#4DAF4A60", "#4DAF4A60"),
      pad = 5,
      thickness = 10,
      line = list(
        color = "black",
        width = 0.5
      )
    ),

    link = list(
      source = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 11),
      target = c(5, 6, 7, 8, 9, 5, 6, 8, 9, 5, 6, 7, 8, 9, 5, 6, 7, 8, 5, 6, 7, 8, 0, 3, 0, 1, 2, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 0, 1, 2, 4),
      value = c(31, 23, 20, 394, 4, 2, 27, 295, 4, 232, 266, 330, 340, 2, 3, 57, 21, 257, 1, 3, 19, 564, 28, 3, 10, 11, 22, 64, 67, 68, 116, 221, 128, 190, 135, 180, 68, 242, 107, 60, 581, 46, 133, 3, 4, 108, 67, 50, 163, 20)
    )
  ) %>%
  layout(
    title = "Basic Sankey Diagram",
    font = list(
      size = 10
    )
)

Any help is greatly appreciated!

I don’t have a solution for you, but for starters:

  • You have 17 node labels but only 12 nodes referenced in your source and target vectors
  • Idk if this is a problem or not, but you have flows going ‘backwards’ to node 0 and probably other flows moving from a higher order node to a lower order node. I would try to recast those flows with the 0’s (and other lower order nodes) in the source and the nodes of a greater value in the target vector.

edit: I changed your target vector to always have a greater number than the source vector at the same index and the sankey now plots, so i suspect that is the problem. New target vector:

target = c(5, 6, 7, 8, 9, 5, 6, 8, 9, 5, 6, 7, 8, 9, 5, 6, 7, 8, 5, 6, 7, 8, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9,10,10,10,10,10, 11, 11, 12, 12, 12, 12, 13)

There was an error in my links generation which I didn’t notice until I got your message! Thanks a lot!
Regards,
D.