Q:

Setting Size of Detected Communities in R

I'm using igraph community-detection and the community sizes are either too small or too large. Is there any way to specify the size of the detected communities? If not, is there any way for me to manually split or merge communities detected from igraph? Thanks!

A:

Whilst I don't think it's possible to set/specify the size of a community detected by igraph, some of the community detection algorithms allow you to specify how many communities you want (an alternative to splitting/merging).
You can use either the cluster_spinglass() function and set spins to be the number of communities desired. Or use one of the hierarchical methods and then use cut_at() to get the desired number of communities, using the no argument to specify how many communities you want.
Example code:
# Set up your graph object
g <-[an igraph object] # set up your graph

# Use spinglass to create a set number of communities
sg <- g %>% cluster_spinglass(spins = 10) # produces 10 communities using spinglass algorithm

# Use hierarchical methods and cut_at to create a set number of communities 
walk <- g %>% cluster_walktrap() %>% cut_at(no = 10) 
eb <- g %>% cluster_edge_betweenness() %>% cut_at(no = 10)

Note that the spinglass method will give you back a communities object, whereas the cut_at method simply gives you back the community indices for all nodes in the graph (i.e. a simple numeric vector).
You can find more details on the communities help page.

