根据节点id选择所有连接节点的子图

时间:2018-04-10 12:20:23

标签: r igraph

我有一个由3个子图组成的图表:

 person1 <- c(0, 0, 1, 3, 6)
 person2 <- c(1, 2, 4, 5, 7)


 id <- c(0,1, 2, 3, 4, 5, 6, 7)
 person <- c("Marc", "Marc","Eric", "Alan", "Henri", "Adele", "Wil", "Marc")

 nodes <- data.frame(id, person,  stringsAsFactors = FALSE)

 union_edges <- data.frame(person1, person2)


 library(igraph)
 family_tree <- graph_from_data_frame(union_edges)
 plot(family_tree)

enter image description here

如何根据节点ID选择所有连接节点的子图?例如,选择一个节点(1,2,4或0)将为我提供这个子图:

enter image description here

1 个答案:

答案 0 :(得分:1)

您查看已连接的群集:

comps <- components(family_tree)
ids <- names(comps$membership)[comps$membership == comps$membership["1"]]
#[1] "0" "1" "2" "4"

plot(induced_subgraph(family_tree, V(family_tree)[ids]))

enter image description here