Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 单个群落的个体着色_R_Colors_Cluster Analysis_Igraph - Fatal编程技术网

R 单个群落的个体着色

R 单个群落的个体着色,r,colors,cluster-analysis,igraph,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_lo

我有这段代码

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)
因此,使用图形的颜色,但我会松开组标记


如何根据
长度更改每个社区的颜色和组标记(社区[1])==1

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


识别每个组>1中的顶点,并将它们作为列表传递给
标记.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)

r感谢您的回复。对于
length>1
的社区的
mark.group
呢?对不起,我错过了那部分。看起来@paqmo已经涵盖了这一点。遗憾的是,这样一来,我无法接受你的任何一个答案,他没有涵盖你解决的部分:D,但a+1谢谢你的回答。使用
rle
的一种非常有趣的方法+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)
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)