Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

r - How can I plot igraph community with defined colors?

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. Could I custom the community color?
  2. Could I add some text in the community area?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...