【问题标题】:Assign colors to communities in igraph在 igraph 中为社区分配颜色
【发布时间】:2014-07-06 12:16:16
【问题描述】:

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

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)

值得注意的是,没有子图不连接到该图,因为这是较大图的最大连接组件。节点之间的联系比较纠结,少颜色的剧情解读困难的原因。

【问题讨论】:

  • 那么为什么不给vertex.color分配不同的颜色呢?

标签: r igraph sna


【解决方案1】:

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

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 种颜色,您会得到一些非常接近的颜色。

请注意,在 SO 上,强迫他人为您创建示例被认为是不礼貌的。要么提供你的数据集,要么创建一个有代表性的例子并提供它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 2020-01-27
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多