3

I have a relatively simple sankey diagram, generated using networkD3 package inside a shiny app. The problem I'm facing is that, depending on my input data, the dimensions (height and width) of the output diagram, differ significantly. Please have a look at the gif below to see the behavior.

enter image description here

Code of MWE:

library("shiny")
library("networkD3")

ui <- fluidPage(
  column(3, 
         sliderInput("f1", "Factor:",
                     min = 1, max = 100, value = 1, step = 1,
                     animate = animationOptions(interval = 1, loop = FALSE))
         ),
  column(6, sankeyNetworkOutput("mySankeyD", width = "100%", height = "100%")),
  column(3, "")
)

server <- function(input, output) {

  output$mySankeyD <- renderSankeyNetwork({


    myDf <- list(nodes = data.frame(name=c( "A", "B", "C", "D",
                                           "W", "X", "Y", "Z")),
                links = data.frame(source=as.integer(c(0, 0, 0, 0, 1, 2, 3)),
                                   target=as.integer(c(4, 5, 5, 5, 7, 6, 5)),
                                   value =c(1*input$f1, 1, 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,
                  nodePadding = 10, height = 500, width = 500, sinksRight =TRUE,
                  margin = NULL)
  })

}

shinyApp(ui, server)

I tried manipulating input varibales height and width inside sankeyNetworkOutput() function and varibales nodePadding, height, width, sinksRight and margin inside sankeyNetwork(). None seemed to have a congruent effect on the plot size.

I also tried standardizing the values of links dataframe inside myDf like this: value = c(1*input$w1, 4, 1, 5, 1, 5, 3)/(1*input$w1) or value = c(1*input$w1, 4, 1, 5, 1, 5, 3)/sum(c(1*input$w1, 4, 1, 5, 1, 5, 3)) but standardizing didn't make any difference at all. I think it is the relations of the values of value that have an impact on the plot dimensions. But why?

What do I miss? Why do the values of value influence the plot dimensions at all?

Does anybody have a solution or workaround?

jmjr
  • 2,090
  • 2
  • 21
  • 31
  • 1
    Maybe [turn off zooming](https://github.com/christophergandrud/networkD3/pull/148/commits/e3c69cbd72c662dbe64e22c84856efa2f3e71fd0) and adjust the column height to 500px, too. – lukeA Sep 17 '17 at 16:54
  • Thanks for your comment. Unfortunately, adding `zoom = FALSE` to `sankeyNetwork()` produces `ERROR: unused arguments (zoom = FALSE) `. Do I need to update (dev-version) of `networkd3`? – jmjr Sep 17 '17 at 19:41
  • yes @jmjr. However, you need the commit which I referenced; the zoom option is not available in the recent dev version, if I remember correctly. – lukeA Sep 17 '17 at 19:43
  • How sure are you, that the effect is due to zooming being turned on? Btw, adjusting column height to 500px does not change the behavior. – jmjr Sep 17 '17 at 19:49
  • I quickly tried it a few hrs ago and it seemed to work – lukeA Sep 17 '17 at 19:52
  • Ok, good. Is it also possible to update `networkd3` (and the specific commit) on shinyapps.io? Because it is there where I need it to work. – jmjr Sep 17 '17 at 20:18
  • You try it out and tell me. :) – lukeA Sep 18 '17 at 08:09

0 Answers0