Search code examples
visnetwork

visNetwork: Rotate Graph arranged by Sugiyama Layout Algorithm


The following directed example graph is initially arranged with the visIgraphLayout("layout_with_sugiyama") algorithm:

library(dplyr)
library(visNetwork)

### create nodes
nodes <- tibble(id = c (1:13), group = c("D","D","D","A","C","C","C","C","A","A","C","A","A"), 
label = c("only outgoing a","only outgoing b","only outgoing c","only incoming d","e","f","g","h","only incoming i","only incoming j","k","only incoming l","only incoming m")

### create edges
edges <- tibble(id = 1:12, from = c(1,1,2,3,3,7,6,8,8,5,11,11), to = c(5,6,5,4,7,8,8,9,11,10,12,13), arrows = "to")

### visualize graph

visNetwork(nodes, edges, main = "Test") %>% 
  visGroups(groupname = "A", size = 25, color = list(
    background = "#005A83",
    border = "#005A83")) %>%
  visGroups(groupname = "C",size = 20,  color = list(
    background = "#994350",
    border = "#000000")) %>%
  visGroups(groupname = "D", size = 20, color = list(
    background = "#44706F",
    border = "#44706F")) %>%
  visEdges(smooth = F, font = list("size"=5), color = "black") %>% 
  visLegend(width = 0.2) %>%
    visIgraphLayout("layout_with_sugiyama") %>%
  visInteraction(navigationButtons = TRUE) %>%
   visPhysics(enabled = F)

The algorithm arranged the directed graph from the top to the bottom. I would like to visualize it from the left to the right. Hence, i only would like to rotate the graph by 90 degree to the left so that the green start-nodes are at the left and the blue end-nodes at the right. The legend etc. should not be affected.

Anybody an idea?


Solution

  • Found a solution due to bthieurmel on github:

    graph <- visNetwork(nodes, edges, main = "Test") %>% 
      visGroups(groupname = "A", size = 25, color = list(
        background = "#005A83",
        border = "#005A83")) %>%
      visGroups(groupname = "C",size = 20,  color = list(
        background = "#994350",
        border = "#000000")) %>%
      visGroups(groupname = "D", size = 20, color = list(
        background = "#44706F",
        border = "#44706F")) %>%
      visEdges(smooth = F, font = list("size"=5), color = "black") %>% 
      visLegend(width = 0.2) %>%
        visIgraphLayout("layout_with_sugiyama") %>%
      visInteraction(navigationButtons = TRUE) %>%
       visPhysics(enabled = F)
    
    # access to the coordinates of the graph object with graph$nodes$x
    # change the x and y coordinates
    
    coord_y <- graph$x$nodes$y
    graph$x$nodes$y <- graph$x$nodes$x
    graph$x$nodes$x <- coord_y
    
    graph