Saeed VA
Saeed VA

Reputation: 27

R: Split a graph into several graphs

This graph contains 4 communities.

This graph

I want to convert each community to a new graph. How should I do this?

Upvotes: 1

Views: 511

Answers (1)

G5W
G5W

Reputation: 37661

Since you do not provide data, I will use a simplified version of your graph to illustrate.

## Example graph
library(igraph)
g <- graph_from_literal(1-2-3-4-1, 2-5-4,
    2-6, 6-7-10-8-6, 6-9-10)
CL = cluster_louvain(g) 
plot(CL, g)

Communities

In order to graph the individual communities, you can use induced_subgraph to get the subgraphs and then use the like any other graph, including plotting them.

## Get graphs for each community
C1 = induced_subgraph(g, which(membership(CL) == 1))
C2 = induced_subgraph(g, which(membership(CL) == 2))

plot(C1)
plot(C2)

Note: I combined the graphs by hand. They were printed separately.

Communities

Upvotes: 4

Related Questions