如何绘制具有定义颜色的igraph社区? [英] How can I plot igraph community with defined colors?

查看:185
本文介绍了如何绘制具有定义颜色的igraph社区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用下面的代码来生成和绘制社区:

I can use the code below to generate and draw communities:

 wc <- walktrap.community(subgraph)
 modularity(wc)
 membership(wc)

 layout <-layout.fruchterman.reingold(subgraph)

 plot(wc, subgraph,  layout=layout, vertex.label=NA, vertex.size=5, edge.arrow.size=.2)

但是,社区的颜色是自动的,我有两个问题:

However, the colors of the communities are automatic, I have two questions:

  1. 我可以自定义社区颜色吗?
  2. 我可以在社区区域添加一些文本吗?

推荐答案

是的,您可以同时做这两个事情.使用plot.igraph中的参数可以很容易地根据节点所在的模块更改节点的颜色(以及更改模块周围的多边形的颜色).据我所知,向模块中添加文本并不是一件容易的事,据我所知,最简单的解决方案是将文本手动添加至绘图中.

Yes, you can do both of those things. Changing the colors of the nodes according to which module they are in (as well as changing the colors of the polygons around the modules) is straightforward using arguments in plot.igraph. Adding text to modules is not so trivial, and the easiest solution is as far as I know is to add text to the plot manually.

library(igraph)

# Generate random graph and community structure
set.seed(23)
g <- sample_gnm(15, 45)
wc <- walktrap.community(g)

# Plot
par(mfrow=c(1,2), mar=rep(1,4))
layout <-layout.fruchterman.reingold(g)
plot(wc, g, layout=layout, vertex.label=NA, vertex.size=5,  edge.arrow.size=.2)

# Change colors of nodes, polygons, and polygon borders
new_cols <- c("white", "red", "black")[membership(wc)]
plot(wc, g, col=new_cols, mark.border="black", mark.col=c("tan", "pink", "lightgray"), 
    layout=layout, vertex.label=NA, vertex.size=5, edge.arrow.size=.2)

# Add labels
text(c(-1.15, 0.8, 0.9), c(0.35, -0.7, 0.8), c("A", "B", "C"))

这篇关于如何绘制具有定义颜色的igraph社区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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