0

I think the answer to this question is "it's not currently supported", which is fine, but I'm hoping to build a sankey diagram with the nodes aligned horizontally based on their group. I found this answer, which allows it to be done manually after rendering, but I'm hoping for a more programmatic answer.

For example, this is how it looks currently:

links <- data.frame(
  source = c(
    19, 0, 20, 20, 13, 16, 16, 21, 7, 22,
    23, 23, 8, 10, 1, 4, 2, 14, 17, 3, 5, 
    9, 11, 24, 15, 25, 18
  ),
  target = c(
    26, 6, 27, 28, 16, 19, 21, 29, 12, 30, 
    31, 32, 10, 14, 4, 9, 5, 17, 20, 5, 8, 
    11, 13, 33, 18, 34, 25
  ),
  n = c(
    2L, 3L, 2L, 17L, 18L, 2L, 16L, 16L, 2L, 
    6L, 2L, 2L, 22L, 22L, 25L, 21L, 2L, 22L, 
    19L, 20L, 22L, 21L, 18L, 2L, 2L, 4L, 2L
  )
)

nodes <- data.frame(
  label = c(
    "a", "b", "c", "d", "e", "f", "g", "h",
    "i", "j", "k", "l", "m", "n", "o", "p", 
    "q", "r", "s", "t", "u", "v", "w", "x", 
    "y", "z", "A", "B", "C", "D", "E", "F", 
    "G", "H","I"
  ),
  group = c(
    "9.1", "9.1", "9.1", "9.1", "9.2", "9.2", 
    "9.2", "10.1", "10.1", "10.1", "10.2", 
    "10.2", "10.2", "11.1", "11.1", "11.1", 
    "11.2", "11.2", "11.2", "12.1", "12.1", 
    "12.1", "12.1", "12.1", "12.1", "12.1", 
    "12.2", "12.2", "12.2", "12.2", "12.2",
    "12.2", "12.2", "12.2", "12.2"
  )
)

node_colors <- '
  d3.scaleOrdinal() 
  .domain(["9.1", "9.2", "10.1", "10.2", "11.1", "11.2", "12.1", "12.2"]) 
  .range(["red", "red", "orange", "orange", "green", "green", "blue", "blue"])
'

networkD3::sankeyNetwork(
  Links = links,
  Nodes = nodes,
  Source = "source",
  Target = "target",
  Value = "n",
  NodeID = "label",
  NodeGroup = "group",
  colourScale = node_colors,
  fontSize = 12
)

sankey-bad

But I'd like it to look (basically) like this, without making manual alterations after the fact.

sankey-good

Any ideas?

Daniel Anderson
  • 2,394
  • 13
  • 26

0 Answers0