rdata-visualizationigraphvisnetwork

Directly Adding Titles and Labels to Visnetwork


I have the following network graph:

library(tidyverse)
library(igraph)


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, main = "my_graph")

I was able to convert this graph into a "visnetwork" graph:

library(visNetwork)

visIgraph(graph)

Now, I am trying to put a title on this graph:

visIgraph(graph, main = "my title")

Although this doesn't work:

Error in layout_with_fr(graph, dim = dim, ...) : 
  unused argument (main = "my title")

I found this link https://datastorm-open.github.io/visNetwork/legend.html that shows how you can add titles to a "visnetwork" graph :

nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
edges <- data.frame(from = c(1,2), to = c(2,3))

# default, on group
visNetwork(nodes, edges, 
           main = "A really simple example", 
           submain = list(text = "Custom subtitle",
                          style = "font-family:Comic Sans MS;color:#ff0000;font-size:15px;text-align:center;"), 
           footer = "Fig.1 minimal example",
           width = "100%")

This seems to be pretty straightforward, but it requires you to use the "visNetwork()" function instead of the "visIgraph()" function.

Thank you!


Solution

  • We can try this approach if you like

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

    or

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

    and you will see

    enter image description here