调整图表布局

发布于 2025-01-10 06:53:28 字数 1834 浏览 1 评论 0 原文

我有以下图表:

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

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

在此处输入图像描述

我尝试使用“visNetwork”库制作完全相同的图表,但该图表现在显示为“圆形”而不是“随机”:

  visIgraph(graph)

在此处输入图像描述

我尝试研究是否可以使用“visNetwork”库生成“随机排序”图而不是圆形图。例如:

 visIgraph(graph) %>%
 visLayout(randomSeed = 123) 

但这仍然会产生圆形图。

我意外地发现这段代码可以正常工作(https://www .rdocumentation.org/packages/visNetwork/versions/2.1.0/topics/visIgraphLayout):

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

在此处输入图像描述

为什么“随机种子”选项仍然会产生圆形图,但是上面的选项会产生所需的结果吗?对此有解释吗?

谢谢你!

I have the following graph:

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

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

enter image description here

I tried to make this exact same graph using the "visNetwork" library, but the graph now appears "circular" instead of "random":

  visIgraph(graph)

enter image description here

I tried to look into whether its possible to produce a "random ordered" graph instead of a circular graph using the "visNetwork" library. For example:

 visIgraph(graph) %>%
 visLayout(randomSeed = 123) 

But this still results in a circular graph.

I unexpectedly found this code that works instead (https://www.rdocumentation.org/packages/visNetwork/versions/2.1.0/topics/visIgraphLayout):

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

enter image description here

Why does the "random seed" option still produce a circular graph, but the above option produces the desired result? Is there an explanation for this?

Thank you!

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

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

发布评论

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

评论(1

把回忆走一遍 2025-01-17 06:53:28

不确定完全理解您要寻找的内容,但是:

  1. 如果您希望顶点随机放置而不是在圆上,则只需在内部使用参数 layout = "layout_randomly" visIgraph() 函数。

  2. 如果您希望顶点随机放置在圆上,则需要使用 permute() 函数,然后只需添加参数 layout = "layout_circle"visIgraph() 函数内。

请在下面找到一个代表。

Reprex

  • 您的数据
library(dplyr)
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")
  • 建议的代码

    1。随机顶点放置

visIgraph(graph, layout='layout_randomly')

2.圆上的随机顶点放置

permute(graph, permutation = sample(vcount(graph))) %>% 
  visIgraph(layout='layout_in_circle')

reprex 包 (v2.0.1)

Not sure to fully understand what you are looking for but:

  1. If you want the vertices to be placed randomly and not on a circle, you just need to use the argument layout = "layout_randomly" inside the visIgraph() function.

  2. If you want the vertices to be placed randomly and on a circle, you need to use the permute() function and then just add the argument layout = "layout_circle" inside the visIgraph() function.

Please find below a reprex.

Reprex

  • Your data
library(dplyr)
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")
  • Suggested code

    1. Random vertex placement

visIgraph(graph, layout='layout_randomly')

2. Random vertex placement on a circle

permute(graph, permutation = sample(vcount(graph))) %>% 
  visIgraph(layout='layout_in_circle')

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

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