How to specify nodes' positions in visNetwork package in R

后端 未结 1 2003
孤城傲影
孤城傲影 2021-01-14 05:42

I would like to fix the positions of the nodes at (1,0), (0,1), (-1,0), (0,-1) and (0,0). However, it does not not work and my Java knowledge is zero (it seems, that here is

相关标签:
1条回答
  • 2021-01-14 06:30

    You could do

    coords <- matrix(ncol=2, byrow=T, data=c(
      1,0,
      0,1,
      -1,0,
      0,-1,
      0,0))
    opts <- . %>% visOptions(highlightNearest = TRUE) %>%
      visInteraction(navigationButtons = TRUE, dragNodes = FALSE, 
                     dragView = FALSE, zoomView = FALSE) %>%
      visEdges(arrows = 'from') 
    
    visNetwork(nodes, edges, width = "100%") %>%
      visIgraphLayout(layout = "layout.norm", layoutMatrix = coords) %>% 
      opts
    

    or

    nodes$x <- c(1, 0, -1, 0, 0)*100
    nodes$y <- c(0, 1, 0, -1, 0)*100
    visNetwork(nodes, edges, width = "100%") %>% 
      visNodes(fixed = TRUE) %>% 
      opts
    

    Use coords[,2] <- coords[,2]*-1 to flip the vertical axis if necessary.

    0 讨论(0)
提交回复
热议问题