0

I am using the networkd3 package in R to construct Sankey networks. I tried looking through other questions but could not find a related one. By default the nodes vertically align at the top of the view window, like so:

Default Sankey - top vertical alignment

If possible, I want to align them automatically (avoiding the need to click and drag nodes) on the bottom, like this:

Sankey - bottom vertical alignment

The networkD3::sankeyNetwork function does not seem to have any options to do so, and I wonder if there is a JS option available in htmlwidgets::onRender. Any help would be greatly appreciated!

Code to produce the diagram in the first image above:

library(networkD3)

## Create links dataframe
links <- data.frame(
  from = c("A1", "A2", "A3", "A4", "A5"),
  to = c("B", "B", "B", "B", "B"),
  count = c(10, 10, 5, 5, 10)
)

## Create node dataframe
nodes <- data.frame(
  name = unique(
    c(
      as.character(links$from), 
      as.character(links$to)
    )
  )
)

## Add node indices to link data (zero-indexed)
links$source = match(links$from, nodes$name)-1
links$target = match(links$to, nodes$name)-1

## Render Sankey
sankeyNetwork(
  Links = links,
  Nodes = nodes,
  Source = "source",
  Target = "target",
  Value = "count",
  NodeID = "name",
  iterations = 0, 
  sinksRight = FALSE
)
Josh C
  • 43
  • 7

1 Answers1

1

I can't imagine a way to do this without re-writing/overriding the sankey generator function in JavaScript, in which case it would just make more sense to create the sankey yourself in JavaScript rather than using networkD3.

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56