我有以下图表:
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)
I tried to make this exact same graph using the "visNetwork" library, but the graph now appears "circular" instead of "random":
visIgraph(graph)
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)
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!
发布评论
评论(1)
不确定完全理解您要寻找的内容,但是:
如果您希望顶点随机放置而不是在圆上,则只需在内部使用参数
layout = "layout_randomly"
visIgraph()
函数。如果您希望顶点随机放置在圆上,则需要使用
permute()
函数,然后只需添加参数layout = "layout_circle"
在visIgraph()
函数内。请在下面找到一个代表。
Reprex
建议的代码
1。随机顶点放置
2.圆上的随机顶点放置
由 reprex 包 (v2.0.1)
Not sure to fully understand what you are looking for but:
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 thevisIgraph()
function.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 argumentlayout = "layout_circle"
inside thevisIgraph()
function.Please find below a reprex.
Reprex
Suggested code
1. Random vertex placement
2. Random vertex placement on a circle
Created on 2022-02-25 by the reprex package (v2.0.1)