简体   繁体   English

树状图中连接节点的数量

[英]Number of Connected Nodes in a dendrogram

Just started working with the tidygraph and ggraph packages recently and have a relatively simple problem, though, oddly, cannot seem to find an easy solution.最近刚开始使用 tidygraph 和 ggraph 包,遇到了一个相对简单的问题,但奇怪的是,似乎找不到简单的解决方案。 Within a network, how many nodes are connected down from a single parent?在一个网络中,有多少节点从单个父节点向下连接? Would seem to be a fairly simple question, but have struggled to arrive at an answer, especially when there are multiple "parent/child" relationships that need to be unfolded.似乎是一个相当简单的问题,但一直在努力得出答案,尤其是当有多个“父/子”关系需要展开时。

# reproducible example -----------------

library(tidygraph)
library(ggraph)
library(tidyverse)

parent_child <- tribble(
  ~parent, ~child,
        "a", "b",
        "b", "c",
        "b", "d",
        "d", "e",
        "d", "f",
        "d", "g",
        "g", "z"
)

# converted to a dendrogram ------------

parent_child %>% 
  as_tbl_graph() %>% 
  ggraph(layout = "dendrogram") +
  geom_node_point() +
  geom_node_text(aes(label = name),
                 vjust = -1,
                 hjust = -1) +
  geom_edge_elbow()

This result is a network这个结果是一个网络在此处输入图片说明

What I want to know;我想知道的; how many nodes are connected to point "b" when moving out/down (ignoring node "a")?移出/向下时(忽略节点“a”)有多少个节点连接到点“b”? The answer I would expect is 6, or, including "b", then 7.我期望的答案是 6,或者,包括“b”,然后是 7。

I am running this over a network of about 5000 individuals, so filtering individual nodes by name is not a great solution.我在大约 5000 个人的网络上运行它,所以按名称过滤单个节点不是一个很好的解决方案。 No one else in my office is familiar with network analyses, so have been kind of left on my own to figure this out.我办公室里没有其他人熟悉网络分析,所以我只能自己解决这个问题。 Really hope someone has an insight!真的希望有人有见地! In the mean time will keep reviewing the problem and possible solutions :) Thank y'all!同时将继续审查问题和可能的解决方案:) 谢谢大家!

One way would be to use ego_size() from the igraph package.一种方法是使用igraph包中的ego_size() It needs an order parameter but you could use the edge count to capture the maximum possible order of the neighborhood.它需要一个order参数,但您可以使用边数来捕获邻域的最大可能顺序。

library(igraph)

g <- graph_from_data_frame(parent_child)

ego_size(g, order = ecount(g), nodes = "b", mode = "out", mindist = 1)

[1] 6

For multiple nodes, just pass a vector of the nodes of interest:对于多个节点,只需传递感兴趣节点的向量:

ego_size(g, order = ecount(g), nodes = c("b", "d", "g"), mode = "out", mindist = 1)

[1] 6 4 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM