直接向 Visnetwork 添加标题和标签

发布于 2025-01-10 00:42:56 字数 1734 浏览 0 评论 0原文

我有以下网络图:

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

我找到了此链接 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()”函数直接添加标题?

谢谢你!

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.

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

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2025-01-17 00:42:56

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

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

,或者

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

您将看到

在此处输入图像描述

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

东风软 2025-01-17 00:42:56

我无法弄清楚如何使用“visIgraph()”函数来做到这一点 - 但我认为我能够弄清楚如何生成随机图(满足某些条件:根据某些条件生成随机图)并使用常规的“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()”函数吗?

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

It feels great (somewhat) solving my own problem!

  • But is this possible directly with the "visNetwork()" function?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文