如何指定节点'在R的visNetwork包中的职位

时间:2017-12-04 09:40:29

标签: r visnetwork

我想将节点的位置固定在(1,0),(0,1),( - 1,0),(0,-1)和(0,0)。但是,它不起作用,我的Java知识是零(似乎here是关于Java代码的问题)。

有人可以帮忙吗?这是一个例子:

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 个答案:

答案 0 :(得分:2)

你可以做到

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

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

enter image description here

如有必要,请使用coords[,2] <- coords[,2]*-1翻转垂直轴。