Strange plot sizing of interactive sankey diagram within a shiny app

1.4k views Asked by At

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?

0

There are 0 answers