我使用的是UKfaculty数据库中的数据
library(igraph)
library(igraphdata)
library(RColorBrewer)
data("UKfaculty")我使用函数as.undirected()将有向图转换为无向图。
UND_UKfaculty <- as.undirected(UKfaculty)我应用了cluster_louvain函数来获得分组:
lc <- cluster_louvain(UND_UKfaculty)现在,我发现了每个组中有多少节点:
sizes(lc)
Community sizes
1 2 3 4 5
18 19 13 25 6我试图从初始图中删除具有函数delete_vertices的节点数最多的组(第4组)。
我的问题集中在最后一部分。我不知道如何使用函数delete_vertices。
我试过:
ld<-delete_vertices(lc, 4)但是有人提到,delete_vertices(lc, 4)不是一个图形对象。
发布于 2022-11-10 15:12:14
对于群集id,例如4,应该确定相应的顶点。然后这些顶点就可以被移除。请注意,igraph中的顶点ids总是自动从1重新编号到最大值。因此,我们必须保留源图的顶点名。
require(igraph)
##
## step 1
## calculate cluster, show in plot
## community object lc: vertices indexed by cluster id
g1 <- make_tree(20, children = 2, mode = "undirected")
V(g1)$names <- as.character(V(g1))
lc <- cluster_louvain(g1, resolution = quantile(degree(g1))[4] / (ecount(g1) - 1))
plot(g1, mark.groups=lc)
## step2
## calculate the ids of the most densely populated clusters
## and determine the corresponding vertices
sz <- sizes(lc)
max_clusters <- ( which(sz == max(sz)) )
V_to_delete <- unlist(lc[max_clusters])
## step 3
## delete vertices and plot new graph
g2 <- delete_vertices(g1, V_to_delete)
V(g2)$label <- V(g2)$names
dev.new()
plot(g2)https://stackoverflow.com/questions/74380397
复制相似问题