gpt4 book ai didi

r - 提取有关 tidygraph 中特定节点的邻域/子图

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

我在摸索某些在 igraph 中相对简单的 Tidygraph 操作时遇到了一些麻烦。

特别是我想以不同的顺序分析特定的社区。我想我需要为此使用 Morphs,但我还没有让它工作。

library(tidygraph)
library(ggraph)
net <- tibble::tibble(A = letters[1:6],
B = rep(c("x", "y"), each = 3)) %>%
tidygraph::as_tbl_graph()

例如,假设我有以下网络结构:

我想分析关于 x 的邻域。

net %>% 
ggraph(layout = "nicely") +
geom_edge_link() +
geom_node_point(size = 10, fill = "white", shape = 21) +
geom_node_text(aes(label = name)) +
theme_graph()

Full network

iGraph 实现工作如下:

提取节点x的邻域。

v <- net %>% 
tidygraph::as.igraph() %>%
igraph::neighborhood(nodes = "x", order = 1)

通过取消列出 igraph.vs 对象来构建子图

igraph::induced_subgraph(net, vids = unlist(v)) %>% 
tidygraph::as_tbl_graph() %>%
ggraph(layout = "nicely") +
geom_edge_link() +
geom_node_point(size = 10, fill = "white", shape = 21) +
geom_node_text(aes(label = name)) +
theme_graph()

desired neighborhood

如何使用 tidygraph 执行此操作?

以下实现返回相同的错误:

net %>% 
tidygraph::morph(to_local_neighborhood, node = "x", order = 1, mode = "all")

net %>%
to_local_neighborhood(node = "x", order = 1, mode = "all")
Error in if (is.numeric(v) && any(v < 0)) { : missing value where TRUE/FALSE needed

最佳答案

使用一些基本 R 函数比 GitHub-linked solution 稍微迂回一点如果您不熟悉 tidygraph API,但我们可以使用 @camille's insight 获取节点的索引因为要求 node 参数是一个数字。

library(tidygraph)
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
library(ggraph)
#> Loading required package: ggplot2

# Example
net <- tibble::tibble(A = letters[1:6],
B = rep(c("x", "y"), each = 3)) %>%
tidygraph::as_tbl_graph()

# Get row name index as integer because tidygraph requirement
node_tbl <- data.frame(activate(net, nodes)) # Make sure focus on nodes
node_idx <- rownames(node_tbl)[node_tbl$name == "x"]
node_idx <- as.integer(node_idx) # tidygraph needs it as number, not string

net %>%
tidygraph::convert(to_local_neighborhood,
node = node_idx,
order = 1,
mode = "all") %>%
ggraph(layout = "nicely") +
geom_edge_link() +
geom_node_point(size = 10, fill = "white", shape = 21) +
geom_node_text(aes(label = name)) +
theme_graph()

reprex package 创建于 2020-07-03 (v0.3.0)

这里是 GitHub 链接的解决方案,它更多地利用了 tidygraph 的 API,通过在转换中使用 .N() 来引用 which(.N()$ 中的节点表名称 == "x").

library(tidygraph)
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
library(ggraph)
#> Loading required package: ggplot2

# Example
net <- tibble::tibble(A = letters[1:6],
B = rep(c("x", "y"), each = 3)) %>%
tidygraph::as_tbl_graph()

net %>%
tidygraph::convert(to_local_neighborhood,
node = which(.N()$name == "x"),
order = 1,
mode = "all") %>%
ggraph(layout = "nicely") +
geom_edge_link() +
geom_node_point(size = 10, fill = "white", shape = 21) +
geom_node_text(aes(label = name)) +
theme_graph()

reprex package 创建于 2020-07-03 (v0.3.0)

关于r - 提取有关 tidygraph 中特定节点的邻域/子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57515804/

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