如何在R中使用igraph删除小社区?

时间:2018-07-10 18:13:21

标签: r networking module igraph

我已经从数据集“ allgenes”中创建了igraph,并基于louvain方法找到了社区模块。

gD <- igraph::simplify(igraph::graph.data.frame(allgenes, directed=FALSE))
lou <- cluster_louvain(gD)

在绘制模块时,我注意到有几个小社区我希望删除。如何删除包含5个以下节点的社区?

plot(lou, gD, vertex.label = NA, vertex.size=5, edge.arrow.size = .2)

包含杰出模块的图:

img

3 个答案:

答案 0 :(得分:3)

由于您没有提供示例,因此我将使用随机生成的数据进行说明。

## First create an example like yours
library(igraph)
set.seed(123)
gD = erdos.renyi.game(50,0.05)
lou <- cluster_louvain(gD)
LO = layout_with_fr(gD)
plot(lou, gD, vertex.label = NA, vertex.size=5, 
    edge.arrow.size = .2, layout=LO)

G1

## identify which communities have fewer than 5 members
Small = which(table(lou$membership) < 5)

## Which nodes should be kept?
Keep = V(gD)[!(lou$membership %in% Small)]

## Get subgraph & plot
gD2  = induced_subgraph(gD, Keep)
lou2 = cluster_louvain(gD2)
LO2 = LO[Keep,]
plot(lou2, gD2, vertex.label = NA, vertex.size=5, 
    edge.arrow.size = .2, layout=LO2)

G2

小社区已被移除

答案 1 :(得分:0)

如果要在保留其他现有社区的同时删除社区,则无法创建带有想要保留的顶点的诱导子图并将其聚集在子图上,因为生成的社区很可能会发生变化。

一种可行的方法是手动将社区对象子集化。

此外,如果要绘制原始图形,社区和新图形,并在所有地方保持相同的颜色,则必须执行几个附加步骤。

suppressPackageStartupMessages(library(igraph))
set.seed(123)

g <- erdos.renyi.game(50, 0.05)
c <- cluster_louvain(g)
l <- layout_with_fr(g)
c_keep_ids <- as.numeric(names(sizes(c)[sizes(c) >= 5]))
c_keep_v_idxs <- which(c$membership %in% c_keep_ids)

g_sub <- induced_subgraph(g, V(g)[c_keep_v_idxs])
# igraph has no direct functionality to subset community objects so hack it
c_sub <- c
c_sub$names <- c$names[c_keep_v_idxs]
c_sub$membership <- c$membership[c_keep_v_idxs]
c_sub$vcount <- length(c_sub$names)
c_sub$modularity <- modularity(g_sub, c_sub$membership, E(g_sub)$weight)

par(mfrow = c(1, 2))
plot(c, g,
  layout = l,
  vertex.label = NA,
  vertex.size = 5
 )
plot(c_sub, g_sub,
  col = membership(c)[c_keep_v_idxs],
  layout = l[c_keep_v_idxs, ],
  mark.border = rainbow(length(communities(c)), alpha = 1)[c_keep_ids],
  mark.col = rainbow(length(communities(c)), alpha = 0.3)[c_keep_ids],
  vertex.label = NA,
  vertex.size = 5
)
par(mfrow = c(1, 1))

communities plots

答案 2 :(得分:0)

请允许我补充一下。我想在可视化时从小社区中“删除”颜色,但将它们保留在图表中。例如我有很多孤立的东西,这会造成一些视觉混乱,而我有一个非常有趣的核心组件,查看它们可以很好地表示。

我从上面的代码开始。不是问题,因为我不想要子图:

Small = which(table(g_community$membership) < 2)
g_community$membership[g_community$membership %in% Small] <- 999

这很有效,但有没有更聪明的方法来做到这一点?