3

I wish to create an interactive Sankey diagram using HTML widgets/Javascript within R Markdown, so am looking at using the networkD3 package. The main interactivity I wish to utilise is clicking on the nodes to have the pathways emanating from it being highlighted to the user.

I previously created static Sankey diagrams using the Riverplot package, in which you can specify the relative positions of each column of nodes, and the order of nodes within that column.

Back to the networkD3 offering, I'm not sure whether I can control relative node location (both vertically and horizontally) in the same, easy way. I've found a few posts that seem to relate to my requirement, but it's not clear to me which (if any) have already been implemented within the package, and what's on the 'wish list'

The posts that I found are: nodesort, fixed nodes and sequence explorer

So, my question is whether anyone can advise on the best R package or technique to use in order to create an interactive HTML widget based sankey diagram with initial (relative) node location control?

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Nevil
  • 161
  • 1
  • 11
  • The primary purpose of `networkD3` is to automatically optimize the layout of its plots, so manually positioning nodes is fighting against that, i.e. it’s not easy and not part of it’s built in functionality. That being said, if you make a minimal reproducible example with specific requirements, I’ll see if I can help. – CJ Yetman Apr 08 '18 at 14:02
  • Hello CJ. I'm still at the 'planning stage' so, sadly, I don't have an example to experiment with at present. I hear your point about the optimisation feature of the networkD3 package, and I am indeed therefore seeking to restrict some of that optimisation. It was possible using the Riverplot package, but there was no interactivity on hand for those creations. So, if I want more, it seems I have less control! – Nevil Apr 08 '18 at 15:02
  • If you use the `iterations = 0` option with `sankeyNetwork`, it effectively disables the optimization. – CJ Yetman Apr 08 '18 at 15:29
  • Hello CJ. So with iterations = 0, do you know if it would place the nodes in the order that they appear in the submitted dataframe? Or are they located by some other mechanism? – Nevil Apr 08 '18 at 16:38
  • 1
    Hello CJ. I may have found an answer to my own question, here: https://stackoverflow.com/questions/46164391/how-to-sort-source-and-or-target-nodes-in-a-sankey-diagram-within-a-shiny-app – Nevil Apr 08 '18 at 16:55
  • 1
    Possible duplicate of [How to sort source and/or target nodes in a sankey diagram within a shiny app?](https://stackoverflow.com/questions/46164391/how-to-sort-source-and-or-target-nodes-in-a-sankey-diagram-within-a-shiny-app) – jmjr Aug 27 '18 at 12:11

1 Answers1

6

You can effectively disable the automatic layout optimization of the sankeyNetwork() function in networkD3 by using the iterations = 0 argument. Using that, the nodes will be plotted in the same order as they are in the data frame.

for example...

library("networkD3")

myDf <- list(
    nodes=data.frame(name=c( "A", "B", "C", "D", "E",
                             "V", "W", "X", "Y", "Z")),
    links=data.frame(source=as.integer(c(0, 1, 2, 3, 3, 4, 4)),
                     target=as.integer(c(7, 6, 7, 8, 7, 5, 9)),
                     value =           c(1, 4, 1, 5, 1, 5, 3)
    )
)

sankeyNetwork(Links = myDf$links, Nodes = myDf$nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              units = "TWh", fontSize = 25, nodeWidth = 30, 
              fontFamily = "sans-serif", iterations = 0)

sanky plot with same order as in the data frame

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