调整图形布局
Adjusting Graph Layouts
我有下图:
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)
为什么“随机种子”选项仍然生成圆形图,但上面的选项生成所需的结果?对此有解释吗?
谢谢!
不确定是否完全理解您要查找的内容,但是:
如果你想让顶点随机放置,而不是在一个圆上,你只需要在visIgraph()
函数中使用参数layout = "layout_randomly"
。
如果你想让顶点随机放置在一个圆上,你需要使用permute()
函数,然后在[=]中添加参数layout = "layout_circle"
14=]函数。
请在下面找到一个代表。
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 package (v2.0.1)
于 2022-02-25 创建
我有下图:
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)
为什么“随机种子”选项仍然生成圆形图,但上面的选项生成所需的结果?对此有解释吗?
谢谢!
不确定是否完全理解您要查找的内容,但是:
如果你想让顶点随机放置,而不是在一个圆上,你只需要在
visIgraph()
函数中使用参数layout = "layout_randomly"
。如果你想让顶点随机放置在一个圆上,你需要使用
permute()
函数,然后在[=]中添加参数layout = "layout_circle"
14=]函数。
请在下面找到一个代表。
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 package (v2.0.1)
于 2022-02-25 创建