gpt4 book ai didi

r - 将 Igraph 转换为 VisNetwork

转载 作者:行者123 更新时间:2023-12-05 03:28:09 24 4
gpt4 key购买 nike

我有使用“igraph”库制作的网络图:

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)
(edge_fac <- forcats::as_factor(get.edgelist(graph)[,1]))
n2 <- as.integer(factor(data$name,levels = levels(edge_fac)))


V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")
V(graph)$label <- paste0(data$name,"\n\n\n",n2)
plot(graph, layout=layout.circle, edge.arrow.size = 0.2, main = "my_graph")

是否可以通过某种方式将上面的图转换为“visnetwork”图,使其看起来像这样?

我知道有一个函数 ( visIgraph() ) 用于将“igraph”图转换为“visnetwork”图:https://www.rdocumentation.org/packages/visNetwork/versions/2.1.0/topics/visNetwork-igraph

但我不确定是否可以将第一个“igraph”图(带有“数字”和“文本”标签)转换为交互式“visnetwork”图。

我试着用下面的代码来做到这一点:

visIgraph(graph)

但这会创建一个没有“数字”标签的交互式图表。

这有可能吗?

谢谢!

最佳答案

如何使用 visNetwork 创建图形?然后,您可以在节点内添加编号和名称作为标签。

library(tidyverse)
library(visNetwork)

set.seed(123)
n=15
data = data.frame(tibble(id = paste(1:n)))

relations = data.frame(tibble(
from = sample(data$id),
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" )
data$shape ='circle'
data$label = paste0(data$id,'\n',data$name)
data$color = ifelse(data$id==1, 'red', 'orange')

visNetwork(data, relations, width = "100%") %>%
visEdges(arrows =list(to = list(enabled = TRUE))) %>%
visIgraphLayout(layout = "layout_in_circle")

Graph

关于r - 将 Igraph 转换为 VisNetwork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71287944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com