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

Extracting neighborhoods / subgraphs about specific nodes in 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()

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()

如何使用 tidygraph 执行此操作?

以下实现return同样报错:

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 in case you're not as familiar with the tidygraph API, but we can get the index of the node using 要求 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 (v0.3.0)

于 2020-07-03 创建

这是 GitHub 链接的解决方案,它更多地利用了 tidygraph 的 API,通过在转换中使用 .N() 来引用 [=] 中的节点 table 14=].

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 (v0.3.0)

于 2020-07-03 创建