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:
If possible, I want to align them automatically (avoiding the need to click and drag nodes) on the bottom, like this:
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
)