直接给 Visnetwork 添加标题和标签

Directly Adding Titles and Labels to Visnetwork

我有以下网络图:

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

我能够将此图转换为“visnetwork”图:

library(visNetwork)

visIgraph(graph)

现在,我想给这张图加上一个标题:

visIgraph(graph, main = "my title")

虽然这不起作用:

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

我发现这个 link https://datastorm-open.github.io/visNetwork/legend.html 展示了如何向“visnetwork”图表添加标题:

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

这看起来很简单,但它要求您使用“visNetwork()”函数而不是“visIgraph()”函数。

谢谢!

我无法弄清楚如何使用“visIgraph()”函数执行此操作 - 但我认为我能够弄清楚如何生成随机图(满足特定条件:Generating Random Graphs According to Some Conditions)并使用常规的“visNetwork()”函数,然后在此图上放置一个标题:

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

解决我自己的问题感觉很棒(有点)!

  • 但这是否可以直接使用“visNetwork()”函数?

如果您愿意,我们可以尝试这种方法

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

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

你会看到