3

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')
Christoph
  • 6,841
  • 4
  • 37
  • 89

1 Answers1

3

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

enter image description here

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

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • 1
    Ok. But do you understand, what the `visNodes(x =, y=)` arguments mean? – Christoph Dec 04 '17 at 10:21
  • And it also seems that `fixed = TRUE, physics = TRUE` is not needed. – Christoph Dec 04 '17 at 10:27
  • @Christoph Did not see that actually, my bad. Seems as if it means what you think it means. Add those to your data frame and `visNodes` takes it from there: `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)`. However, I dunno what the limits of the default coordinate system are. – lukeA Dec 04 '17 at 10:31
  • @lukeA, please check the post on similar lines, I need help here. https://stackoverflow.com/questions/48108231/using-visnetwork-to-dynamically-update-nodes-in-r/48108336#48108336 – Ashmin Kaul Jan 05 '18 at 06:50