在igraph中为社区分配颜色 [英] Assign colors to communities in igraph

查看:71
本文介绍了在igraph中为社区分配颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在igraph中使用fastgreedy.community检测算法在R中生成社区.该代码返回了12个社区,但是在绘制时很难识别它们,因为它返回的颜色数量有限.如何绘制十二种不同颜色的图形?

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.

推荐答案

要获得八种以上的颜色,您需要创建自己的调色板,并使用 membership(fc)作为该调色板的索引

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)

我使用了内置的彩虹调色板,但是还有许多其他创建调色板的方法.事实是,使用8至10种以上的颜色,您将获得彼此非常接近的颜色.

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.

请注意,在SO上强迫他人为您创建示例是不礼貌的.提供您的数据集,或者创建一个具有代表性的示例并提供该示例.

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.

这篇关于在igraph中为社区分配颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆