rdata-visualizationigraphvisnetwork

Understanding "list" and "do.call" commands


Over here (Directly Adding Titles and Labels to Visnetwork), I learned how to directly add titles to graphs made using the "visIgraph()" function:

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")

toVisNetworkData(graph) %>%
  c(., list(main = "my title", submain = "subtitle")) %>%
  do.call(visNetwork, .)

enter image description here

I also learned how to arrange the graph in a random layout:

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

enter image description here

Now, I want to combine these two codes so I can keep the titles (via the visIgraph() function) and the random layout. This is the end goal that I am trying to achieve:

enter image description here

I tried to do this by combining these codes in different ways - but nothing seems to be working:

    g = visIgraph(graph) %>%
        visIgraphLayout(layout = "layout_in_circle") %>%
        visOptions(highlightNearest = list(enabled = T, hover = T), 
                   nodesIdSelection = T)
    
    toVisNetworkData(g) %>%
        c(., list(main = "my title")) %>%
        do.call(visNetwork, .)

Error in toVisNetworkData(g) : igraph must be a igraph object

toVisNetworkData(graph) %>% 
         c(., list(main = "my title")) %>%  visIgraphLayout(layout = "layout_in_circle") 

Error in visIgraphLayout(., layout = "layout_in_circle") : graph must be a visNetwork object

do.call(visNetwork, .)

Error in do.call(visNetwork, .) : object '.' not found

Does anyone know what I am doing wrong? I think I am not correctly understanding how the "do.call" and "list" commands are supposed to be used?

Thank you!


Solution

  • Please find below one possible solution.

    Reprex

    library(dplyr)
    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")
    
    toVisNetworkData(graph) %>%
      c(., list(main = "my title", submain = "subtitle")) %>%
      do.call(visNetwork, .) %>%
      visIgraphLayout(layout = "layout_in_circle") %>% 
      visEdges(arrows = 'to') 
    

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