2017-01-20 72 views
2

我的代碼單一社區

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=NULL)頂點社區(length 1),我知道你可以通過使用$color來操作「正常」圖的顏色,但我沒有fi nd igraph文檔中的任何提示如何處理每個社區。

也有不使用社區

plot(trimmed_network, ...) 

繪製從而使用圖形的顏色選擇,但後來我就輸了組標記。

我怎樣才能改變基於length(communities[1]) == 1

回答

2

標識頂點每組> 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

+0

謝謝你的迴應。使用'rle'非常有趣的方法。 +1解決組標記,不能接受,因爲它缺少單頂點顏色禁用。 – thatsIch

+0

我完全忽略了那部分。我的錯。那麼,我將編輯響應並添加代碼以將這些隔離頂點着色爲白色。爲了完整:)。 – paqmo

+0

我接受了編輯的答案。 – thatsIch

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

+0

感謝您的答覆。長度大於1的社區的mark.group呢? – thatsIch

+0

對不起,我錯過了那部分。它看起來像@paqmo已經覆蓋了它。 – eipi10

+0

可悲的是,這種方式我不能接受你的答案,他沒有涵蓋你解決的部分:D,但採取+1 – thatsIch