简体   繁体   English

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

[英]How to remove small communities using igraph in R?

I have created my igraph from my dataset "allgenes", and found community modules based on the louvain method.我从我的数据集“allgenes”创建了我的igraph ,并找到了基于 louvain 方法的社区模块。

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

Plotting the modules, I note that there are several small communities that I wish to remove.在绘制模块时,我注意到有几个我希望删除的小社区。 How would I remove communities containing 5 nodes or less?我将如何删除包含 5 个或更少节点的社区?

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

Plot with distinguished modules:具有不同模块的绘图:

图片

Since you do not provide an example, I will illustrate with randomly generated data.由于您没有提供示例,我将用随机生成的数据进行说明。

## 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

The small communities have been removed小社区已被移除

If you want to remove communities while maintaining the other existing communities you cannot create an induced subgraph with vertices you want to keep and cluster on the subgraph because the resulting communities can very likely change.如果您想在保留其他现有社区的同时删除社区,则无法创建包含要保留的顶点并在子图上聚类的诱导子图,因为生成的社区很可能会发生变化。

A workable approach would be to manually subset the communities object.一种可行的方法是手动对社区对象进行子集化。

Also, if you want to plot the original graph and communities and new ones and maintain the same colors everywhere you have to do a couple additional steps.此外,如果您想绘制原始图形和社区以及新图形并在任何地方保持相同的颜色,您必须执行一些额外的步骤。

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))

社区地块

Allow me to add to this.请允许我补充一下。 I want to "remove" the color from small communities when visualizing, but keep them in the graph.我想在可视化时从小社区中“删除”颜色,但将它们保留在图表中。 eg I have a lot of isolates and that makes for some visual clutter while I have a very interesting core component, where looking at them gives a good representation.例如,我有很多隔离,这会造成一些视觉混乱,而我有一个非常有趣的核心组件,查看它们可以很好地表示。

I am starting with the code above.我从上面的代码开始。 Not an issue, because I do not want subgraphs:不是问题,因为我不想要子图:

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

This works well enough, but is there a smarter way to do this?这工作得很好,但是有没有更聪明的方法来做到这一点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM