3

我想将节点的位置固定在 (1,0)、(0,1)、(-1,0)、(0,-1) 和 (0,0)。但是,它不起作用,我的 Java 知识为零(似乎,这关于 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')
4

1 回答 1

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

在此处输入图像描述

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

于 2017-12-04T10:15:17.910 回答