How to specify nodes' positions in visNetwork package in R

末鹿安然 提交于 2019-12-19 09:49:33

问题


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 ist the question concering the Java code).

Can anybody help? Here is an example:

require(visNetwork, quietly = TRUE)
nodes <- data.frame(id = 1:5)
                    # x = c(1, 0, -1, 0, 0), 
                    # y = c(0, 1, 0, -1, 0))
edges <- data.frame(from = c(1,2), to = c(1,3))

visNetwork(nodes, edges, width = "100%") %>%
  visNodes(x = c(1, 0, -1, 0, 0), 
           y = c(0, 1, 0, -1, 0), fixed = TRUE, physics = TRUE) %>%
  visOptions(highlightNearest = TRUE) %>%
  visInteraction(navigationButtons = TRUE, dragNodes = FALSE, 
                 dragView = FALSE, zoomView = FALSE) %>%
  visEdges(arrows = 'from')

回答1:


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.



来源:https://stackoverflow.com/questions/47630378/how-to-specify-nodes-positions-in-visnetwork-package-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!