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

标签 r igraph network-analysis tidygraph

我在摸索某些在 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/

相关文章:

r - 简化此网格,使每行和每列都有 1 个值

r - 在 R 中的大矩阵中添加连续的四/n 个数字

r - 在同一个图中绘制 igraph 包中的多个图

r - 将图形写入文件不保留顶点名称

在 igraph 中重新连线以获取 niter 参数的 R 含义

c# - 在 C# 中捕获和编辑 TCP/UDP 数据包

r - R 中两个极坐标图重叠和堆叠的集成版本

将三列数据框 reshape 为矩阵 ("long"到 "wide"格式)

r - 根据权重可视化节点之间的距离 - 使用 R

r - 子集有向图