我可以使用下面的代码来生成和绘制社区:
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)
但是,社区的颜色是自动的,我有两个问题:
最佳答案
是的,您可以同时做这两件事。使用plot.igraph
中的参数可以很容易地根据节点所在的模块更改节点的颜色(以及更改模块周围多边形的颜色)。据我所知,向模块中添加文本并不是一件容易的事,据我所知,最简单的解决方案是手动向绘图中添加文本。
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"))
关于r - 如何绘制具有定义颜色的igraph社区?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37374355/