Assign colors to communities in igraph

微笑、不失礼 提交于 2021-02-10 20:01:08

问题


I am using the fastgreedy.community detection algorithm in igraph to produce communities in R. The code returns 12 communities, however they are difficult to indentify when plotting because it returns a plot with a limited number of colours. How can I plot this graph with tweleve distinct colours?

l2 <- layout.fruchterman.reingold(largest.component)
ebc.g05 <- fastgreedy.community(largest.component)
plot(largest.component, layout=l2, vertex.color=membership(ebc.g05),
 vertex.size=2, vertex.label=NA)

Worth noting that there are no subgraphs that are not connected to this graph, as this is the largest connected component of a larger graph. The connections between nodes are quite tangled, and the reason for the difficulties in interpreting the plot with few colours.


回答1:


To get more than eight colors you need to create your own color palette and use membership(fc) as in index into that palette.

library(igraph)

# create a sample graph - ## you should have provided this!!! ##
g <- graph.full(5)
for (i in 0:11) {
  g <- g %du% graph.full(5)
  g <- add.edges(g,c(5*i+1,5*(i+1)+1))
}

fc <- fastgreedy.community(g)

colors <- rainbow(max(membership(fc)))
plot(g,vertex.color=colors[membership(fc)], 
     layout=layout.fruchterman.reingold)

I used the built-in rainbow palette, but there are many other ways to create color palettes. The fact is that with more than 8 - 10 colors, you are going to get some that are really close to each other.

Note that on SO it is considered rude to force others to create an example for you. Either provide your dataset, or create a representative example and provide that.



来源:https://stackoverflow.com/questions/24595716/assign-colors-to-communities-in-igraph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!