单个社区的个人着色

时间:2017-01-20 22:03:57

标签: r colors cluster-analysis igraph

我有这段代码

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)

并生成

enter image description here

我想禁用单个顶点社区(color="white")中顶点的着色(mark.groups=NULLlength 1),我知道您可以操纵&#34;正常&#34;的颜色使用$color绘制图表,但我没有在igraph文档中找到任何提示如何按社区处理它。

还可以选择不使用社区密谋

plot(trimmed_network, ...)

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

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

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

2 个答案:

答案 0 :(得分:2)

识别每个组中的顶点&gt; 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)

enter image description here

答案 1 :(得分:1)

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

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

enter image description here