单个社区的单独着色

Individual coloring of single communities

我有这段代码

library(igraph)
library(igraphdata)

data("karate")

g <- karate

# for reproducibility
set.seed(23548723)

network_layout <- layout_with_fr(g)
trimmed_network <- delete.edges(g, which(E(g)$weight < 4))
communities <- cluster_louvain(trimmed_network)

plot(communities, trimmed_network, layout=network_layout)

并生成

我想禁用单个顶点社区(length 1)中顶点的着色(color="white"mark.groups=NULL)并且我知道您可以使用 $color 操作 "normal" 图表的颜色,但我没有在 igraph 文档中找到任何提示如何处理每个社区。

也可以选择不使用社区绘图

plot(trimmed_network, ...)

因此使用图表的颜色,但我会松开组标记。

如何根据 length(communities[1]) == 1

更改每个社区的颜色和分组标记

我们需要找到只有一个成员的社区的数字标识符,并将这些单身社区成员的颜色设置为"white"。

# Get community membership
memb = membership(communities)

# Find number of members in each community
tab = table(memb)

# Set colors for each member. (Adjust these as desired)
col = colors()[2:(length(memb)+1)]

# But for members of communities of one, set the color to white
singles = which(memb %in% as.numeric(names(tab)[tab==1]))
col[singles] = "white"

plot(communities, trimmed_network, layout=network_layout, col=col, mark.groups=NULL)

确定每个组中的顶点 > 1 并将它们作为列表传递给 mark.groups。虽然有点笨拙,但确实有效。

r <- rle(sort(communities$membership))
x <- r$values[which(r$lengths>1)]
y <- r$values[which(r$lengths==1)]
cols <- communities$membership
cols[which(cols %in% y)] <- "white"
grps <- lapply(x, function(x) communities[[x]])
grps <- lapply(1:length(grps), function(x) which(V(g)$name %in% grps[[x]]))
plot(communities, trimmed_network, layout=network_layout, 
 col = cols, mark.groups = grps)