datastorm-open / visNetwork

R package, using vis.js library for network visualization
Other
542 stars 127 forks source link

Bug in v 2.1.0 with visHierarchicalLayout producing illegible visualization? #425

Open bjcarothers opened 2 years ago

bjcarothers commented 2 years ago

(Also posted to StackOverflow 2021/10/19)

I'm updating a Shiny app and am getting a very different visualization output than I have in the past. (Apologies for the lengthy data generation - auto-generated sets don't replicate the problem.) The following code running R 4.1.2 with visNetwork_2.1.0:

library(shiny)
library(visNetwork)

# generate data
nodesDf <- data.frame(id=as.character(c(1,3,4,6,8,9,13,15,16,20,21,23,24,25,27,
                                        28,29,30,33,35,36,37,39,40,41,44,45,46,
                                        48,50,51,53,57,58,60,61,62,65)),
                      label=as.character(c(1,3,4,6,8,9,13,15,16,20,21,23,24,25,27,
                                           28,29,30,33,35,36,37,39,40,41,44,45,46,
                                           48,50,51,53,57,58,60,61,62,65)))
edgesDf <- data.frame(from=as.character(c(6,6,6,8,8,8,8,8,8,9,20,20,20,36,36,36,
                                          36,36,36,36,36,36,36,36,36,36,36,36,36,
                                          36,36,44,44,44,44,44,44,48,50,50,50,50,
                                          50,50,50,50,50,50,50,50,50,50,50,50,50,
                                          50,50,50,50,50,50,50,50,50,50,50,50,51,
                                          51,51,51,51,58,62,1,8,25,1,25,36,44,1,
                                          28,37,44,62,1,8,25,1,1,15,25,36,45,1,
                                          28,60,1,8,25,44,36,1,1,6,28,62,25,1,1,
                                          8,1,25,28,36,50,62,1,8,13,28,36,37,44,
                                          46,1,20,44,57,1,8,25,29,1,25,44,1,8,44,
                                          8,35)),
                      to=as.character(c(3,20,53,16,20,30,46,51,61,65,3,39,53,1,
                                        3,6,9,15,16,21,27,29,30,33,35,37,41,51,
                                        58,60,65,23,24,28,41,46,57,9,3,4,6,9,13,
                                        15,16,20,21,23,24,27,28,29,30,35,39,40,
                                        41,45,48,51,53,57,58,60,61,62,65,3,27,30,
                                        37,53,6,3,3,3,3,4,4,4,4,6,6,6,6,6,9,9,9,
                                        15,20,20,20,20,20,23,23,23,27,27,27,27,
                                        28,35,37,37,37,37,39,40,45,45,46,46,46,
                                        46,46,46,48,48,48,48,48,48,48,50,51,51,
                                        51,51,53,53,53,53,58,58,58,62,62,62,65,65)))

# ui
ui <- fluidPage(
  visNetworkOutput("netPlot")
)

# server
server <- function(input, output){
  output$netPlot <- renderVisNetwork({
    visNetwork(nodesDf, edgesDf) %>%
      visEdges(arrows = list(to = list(enabled=TRUE, scaleFactor = 1.5))) %>%
      visHierarchicalLayout(enabled=TRUE, direction="UD", sortMethod="directed")
  })
}

shinyApp(ui=ui, server=server)

generates the following output: image

The same data with visNetwork_2.0.7 (with a few other parameters for color but substantively the same code; I didn't record the R version) generated this output: image

None of the additional visHierarchicalLayout parameters seem to change anything with the exception of lowering levelSeparation from the default of 150 - that brings the two main levels closer together, but the sub-levels within them sit on top of each other and it's still a mess.

I have a different app with different data running visNetwork_2.0.9 that is behaving just fine but also shows the same problem with v2.1.0, so I suspect the issue cropped up between v2.0.9 and v2.1.0. I'm not doing anything differently and I can't find any documentation stating that the parameters need a different kind of treatment, so this feels like a bug.