qshng
qshng

Reputation: 887

Visualize strongly connected components in R

I have a weighted directed graph with three strongly connected components(SCC). The SCCs are obtained from the igraph::clusters function

library(igraph)
SCC<- clusters(graph, mode="strong")  
SCC$membership
 [1] 9 2 7 7 8 2 6 2 2 5 2 2 2 2 2 1 2 4 2 2 2 3 2 2 2 2 2 2 2 2
SCC$csize
[1]  1 21  1  1  1  1  2  1  1
 SCC$no
[1] 9

I want to visualize the SCCs with circles and a colored background as the graph below, is there any ways to do this in R? Thanks!

enter image description here

Upvotes: 3

Views: 2288

Answers (1)

Anders Ellern Bilgrau
Anders Ellern Bilgrau

Reputation: 10223

Take a look at the mark.groups argument of plot.igraph. Something like the following will do the trick:

# Create some toy data
set.seed(1)
library(igraph)
graph <- erdos.renyi.game(20, 1/20)

# Do the clustering
SCC <- clusters(graph, mode="strong")  

# Add colours and use the mark.group argument
V(graph)$color <- rainbow(SCC$no)[SCC$membership]
plot(graph, mark.groups = split(1:vcount(graph), SCC$membership))

Imgur

Upvotes: 11

Related Questions