在R如何从igraph中删除小社区

时间:2017-06-11 12:26:14

标签: r graph igraph

在R网络图中如何删除两个小社区(两个节点连接一个边缘,没有与其他节点连接,如jane和ike在这个例子中:

library(igraph)
g <- graph_from_literal(Andre----Beverly:Diane:Fernando
                    Beverly--Garth:Ed,
                    Carol----Andre:Diane:Fernando,
                    Diane----Andre:Carol:Fernando:Beverly,
                    Fernando-Carol:Andre:Diane:Heather,
                    Jane-----Ike )
plot(g, vertex.label.color="blue", vertex.label.cex=1.5,
 vertex.label.font=2, vertex.size=25, vertex.color="white",
 vertex.frame.color="white", edge.color="black")

1 个答案:

答案 0 :(得分:3)

这是使用components查找子图然后进行计数的可能解决方案。您还可以查看来自igraph的函数,例如groupssizes,以执行获取子图顶点计数和顶点名称的这些操作。

library(igraph)
g <- graph_from_literal(Andre----Beverly:Diane:Fernando,
                        Beverly--Garth:Ed,
                        Carol----Andre:Diane:Fernando,
                        Diane----Andre:Carol:Fernando:Beverly,
                        Fernando-Carol:Andre:Diane:Heather,
                        Jane-----Ike )

#get all subgraphs
sub_gs <- components(g)$membership

#find which subgraphs have 2 nodes
small_sub <- names(which(table(sub_gs) == 2))

#get names of nodes to rm
(rm_nodes <- names(which(sub_gs == small_sub)))
# [1] "Jane" "Ike" 

#remove nodes by name
g2 <- delete_vertices(g, rm_nodes)