R语言 网络/树数据:计算独立树的数量和每个独立树的平均最大边数

yhived7q  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(97)

我想使用tidygraphggraph绘制网络图。
我有一个更大的tibble,其中的项目通过fromto连接。一些树是连接的(示例中的a0b0)。
我想:
1.计算独立树的数量
1.计算平均最大边数=每个独立树的连接数。平均最大边数应该在“下游”计算,即从a0k2a4,而不是示例数据中的a0b0
示例:

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

# make edges
edges<- tibble(from = c("a0","a1","a2","a3","b0","b1","c0","c1","a2","k1"),
               to = c("a1","a2","a3","a4","b1","a3","c1","c2","k1","k2"))

# makenodes
nodes  <- unique(c(edges$from,edges$to))
tibble(node=nodes,
       label=nodes) -> nodes

# make correct dataframe                 
routes_igraph <- graph_from_data_frame(d = edges,
                                       vertices = nodes,
                                       directed = TRUE)

routes_tidy <- as_tbl_graph(routes_igraph)

#plot network
ggraph(routes_tidy, layout = "tree") + 
  geom_edge_link() + 
  geom_node_point() + 
  theme_graph() +
  geom_node_text(aes(label = label), repel = TRUE)

创建于2023-04-16带有reprex v2.0.2

期望输出

1.给定edgesnodes的独立树的数量:2
1.每个独立树的平均最大边数:3.5, 2

ny6fqffe

ny6fqffe1#

这里有一个方法,它从this SO post借用了一个函数height,修改后可以计算"in"个顶点。

height <- function(v, g) {
  D <- distances(g, to=v, mode="in")
  max(D[D != Inf])
}

cmp <- components(routes_igraph)
sp <- split(names(cmp$membership), cmp$membership)
sub_tree_list <- lapply(sp, \(v) induced.subgraph(routes_igraph, v))
sub_tree_height <- Map(\(g, v) sapply(v, height, g = g), sub_tree_list, sp)

# number of components
length(sp)
#> [1] 2

# height of each sub-tree
sapply(sub_tree_height, max)
#> 1 2 
#> 4 2

创建于2023年4月16日,使用reprex v2.0.2

编辑

为了得到每个初始节点的最大值和每个子树的平均值,这个方法是有效的。

initials_list <- lapply(sp, \(x) x[grep("0", x)])
sub_tree_max_height <- Map(\(g, v) sapply(v, height, g = g), sub_tree_list, initials_list)
sapply(sub_tree_max_height, mean)
#>   1   2 
#> 3.5 2.0

创建于2023-04-16带有reprex v2.0.2

相关问题