library(igraph)
g = make_graph("Zachary")
coords = layout_with_fr(g)
plot(g, layout=coords,
vertex.label=NA, vertex.size=6, vertex.color = "blue")

# spectral community detection
c = cluster_leading_eigen(g)
# modularity measure
modularity(c)
## [1] 0.3934089
# number of communities
length(c)
## [1] 4
# size of communities
sizes(c)
## Community sizes
## 1 2 3 4
## 7 12 9 6
# memberships of nodes
membership(c)
## [1] 1 3 3 3 1 1 1 3 2 2 1 1 3 3 2 2 1 3 2 3 2 3 2 4 4 4 2 4 4 2 2 4 2 2
# inter-community crossing edges
crossing(c, g)
## [1] TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE
## [13] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
## [25] FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
## [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
## [49] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
## [61] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE
## [73] FALSE FALSE FALSE TRUE TRUE FALSE
# modularity matrix (why membership? Possible bug)
B = modularity_matrix(g, membership(c))
# first row
round(B[1,], 2)
## [1] -1.64 0.08 -0.03 0.38 0.69 0.59 0.59 0.59 0.49 -0.21 0.69 0.90
## [13] 0.79 0.49 -0.21 -0.21 -0.21 0.79 -0.21 0.69 -0.21 0.79 -0.21 -0.51
## [25] -0.31 -0.31 -0.21 -0.41 -0.31 -0.41 -0.41 0.38 -1.23 -1.74
sum(B[1,])
## [1] 4.440892e-16
# plot communities with shaded regions
plot(c, g, layout=coords)

# plot communities without shaded regions
plot(g, vertex.color=membership(c), layout=coords)
