2

我在处理 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 做到这一点?

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

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
4

1 回答 1

1

如果你不熟悉 tidygraph API,使用一些基本的 R 函数比使用GitHub 链接的解决方案更迂回一些,但是我们可以使用@camille 的洞察力来获取节点的索引,因为它要求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 包(v0.3.0)于 2020-07-03 创建

.N()这是 GitHub 链接的解决方案,它通过在转换中使用 tidygraph 的 API来引用which(.N()$name == "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 包(v0.3.0)于 2020-07-03 创建

于 2020-07-03T21:00:32.520 回答