rdata-visualizationigraphvisnetwork

Adjusting Graph Layouts


I have the following graph:

library(tidyverse)
library(igraph)
library(visNetwork)

set.seed(123)
n=15
data = data.frame(tibble(d = paste(1:n)))

relations = data.frame(tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
))

data$name = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )

graph = graph_from_data_frame(relations, directed=T, vertices = data) 

V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

enter image description here

I tried to make this exact same graph using the "visNetwork" library, but the graph now appears "circular" instead of "random":

  visIgraph(graph)

enter image description here

I tried to look into whether its possible to produce a "random ordered" graph instead of a circular graph using the "visNetwork" library. For example:

 visIgraph(graph) %>%
 visLayout(randomSeed = 123) 

But this still results in a circular graph.

I unexpectedly found this code that works instead (https://www.rdocumentation.org/packages/visNetwork/versions/2.1.0/topics/visIgraphLayout):

visIgraph(graph) %>%
    visIgraphLayout(layout = "layout_in_circle") %>%
    visOptions(highlightNearest = list(enabled = T, hover = T), 
               nodesIdSelection = T)

enter image description here

Why does the "random seed" option still produce a circular graph, but the above option produces the desired result? Is there an explanation for this?

Thank you!


Solution

  • Not sure to fully understand what you are looking for but:

    1. If you want the vertices to be placed randomly and not on a circle, you just need to use the argument layout = "layout_randomly" inside the visIgraph() function.

    2. If you want the vertices to be placed randomly and on a circle, you need to use the permute() function and then just add the argument layout = "layout_circle" inside the visIgraph() function.

    Please find below a reprex.

    Reprex

    library(dplyr)
    library(igraph)
    library(visNetwork)
    
    set.seed(123)
    n=15
    data = data.frame(tibble(d = paste(1:n)))
    
    relations = data.frame(tibble(
      from = sample(data$d),
      to = lead(from, default=from[1]),
    ))
    
    # data$name = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )
    
    graph = graph_from_data_frame(relations, directed=T, vertices = data) 
    
    V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")
    
    visIgraph(graph, layout='layout_randomly')
    

    2. Random vertex placement on a circle

    permute(graph, permutation = sample(vcount(graph))) %>% 
      visIgraph(layout='layout_in_circle')
    

    Created on 2022-02-25 by the reprex package (v2.0.1)