datastorm-open / visNetwork

R package, using vis.js library for network visualization
Other
542 stars 127 forks source link

Directly Adding Titles and Labels to Visnetwork.VisIgraph #436

Open swaheera opened 2 years ago

swaheera commented 2 years ago

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)

image

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.

Is it possible to directly add titles using the "visIgraph()" function?

Thank you!

Possible Solution:

I was not able to figure out how to do this with "visIgraph()" function - but I think I was able to figure out how to generate a random graph (meeting certain conditions: Generating Random Graphs According to Some Conditions) and using the regular "visNetwork()" function and then place a title on this graph:

n=15

data = data.frame(id = 1:n)
data$color = ifelse(data$id == 1, "Red", "Orange")
data$label = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )

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

visNetwork(data, relations, main = "my graph") %>%  visEdges(arrows = "to")

image