# install.packages(asnipe)
library(asnipe)
library(igraph) # assuming you already have igraph installed
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
# create a graph from Shizuka et al (2014) network of golden-crowned sparrows
assoc=as.matrix(read.csv("https://dshizuka.github.io/networkanalysis/SampleData/Sample_association.csv", header=T, row.names=1))
gbi=t(assoc) #transpose the data
mat=get_network(t(assoc), association_index="SRI") # adjacency matrix based on "simple ratio index" (measure for animal network analysis)
## Generating 25 x 25 matrix
g.sparrow=graph_from_adjacency_matrix(mat, "undirected", weighted=T) # igraph object
plot(g.sparrow, edge.width=E(g.sparrow)$weight, vertex.label="", main = "Network of Golden-Crowned Sparrows") # note weighted edges
The purpose of community detection is to analyze the structure of a network and understand the communities that contribute to the network. A community is defined as a “subset of nodes within the graph that are denser than connections with the rest.” These properties tells us that these specific nodes are contributing to the cohesion of the network. This is important because it tells the foundations of the network. These foundations can then lead to important information about how to improve or change the network in question. Finding these sort of things will help identify the sections of the community that are helping or hindering the flow of the network.
# modularity, length, membership, and size of the community structure
fast <- cluster_fast_greedy(g.sparrow) # fast greedy graph object
# modularity
modularity(fast)
## [1] 0.609246
# number of communities
length(fast)
## [1] 4
# community membership for each node
membership(fast)
## 23820 23726 23831 23763 23772 23770 23771 23777 23774 23860 23779 23773 23862
## 4 4 4 4 1 1 3 2 2 1 2 2 2
## 23857 23871 23853 23732 23734 23756 23759 23768 23758 23781 23815 23809
## 1 1 4 2 1 3 3 3 3 1 1 1
# number of nodes in each community
sizes(fast)
## Community sizes
## 1 2 3 4
## 9 6 5 5
# Plotting
par(mfrow = c(1,3))
# dendrogram
set.seed(5)
plot_dendrogram(fast) # provides a dendrogram of the hierarchical community structure
# default color
set.seed(5)
plot(g.sparrow, vertex.color=membership(fast), main="Fastgreedy", sub=paste("Modularity:", round(modularity(fast), 2)), vertex.label=NA)
# grouping
set.seed(5)
plot(fast,g.sparrow,vertex.label.color="black", vertex.size=14,
vertex.label.size=1, vertex.label = NA)
gv <- cluster_edge_betweenness(g.sparrow, modularity = TRUE)
## Warning in cluster_edge_betweenness(g.sparrow, modularity = TRUE): At
## vendor/cigraph/src/community/edge_betweenness.c:498 : Membership vector will be
## selected based on the highest modularity score.
# graph object for edge betweenness
# modularity, length, membership, and size of the community structure
# modularity
modularity(gv)
## [1] 0.5662083
# number of communities
length(gv)
## [1] 3
# community membership for each node
membership(gv)
## 23820 23726 23831 23763 23772 23770 23771 23777 23774 23860 23779 23773 23862
## 1 1 1 1 2 3 2 2 2 3 2 2 2
## 23857 23871 23853 23732 23734 23756 23759 23768 23758 23781 23815 23809
## 3 3 1 2 2 2 2 2 2 3 3 3
# number of nodes in each community
sizes(gv)
## Community sizes
## 1 2 3
## 5 13 7
# Plotting
par(mfrow = c(1,3))
# dendrogram
set.seed(5)
plot_dendrogram(gv) # provides a dendrogram of the hierarchical community structure
# default color
set.seed(5)
plot(g.sparrow, vertex.color=membership(fast), main="Edge-Betweenness", sub=paste("Modularity:", round(modularity(gv), 2)), vertex.label=NA)
# grouping
set.seed(5)
plot(gv,g.sparrow,vertex.label.color="black", vertex.size=14,
vertex.label.size=1, vertex.label = NA)
multi <- cluster_louvain(g.sparrow) # Louvain graph object
# modularity
modularity(multi)
## [1] 0.609246
# number of communities
length(multi)
## [1] 4
# community membership for each node
membership(multi)
## 23820 23726 23831 23763 23772 23770 23771 23777 23774 23860 23779 23773 23862
## 1 1 1 1 2 2 3 4 4 2 4 4 4
## 23857 23871 23853 23732 23734 23756 23759 23768 23758 23781 23815 23809
## 2 2 1 4 2 3 3 3 3 2 2 2
# number of nodes in each community
sizes(multi)
## Community sizes
## 1 2 3 4
## 5 9 5 6
par(mfrow = c(1,2))
# default color
set.seed(5)
plot(g.sparrow, vertex.color=membership(fast), main="Louvain", sub=paste("Modularity:", round(modularity(multi), 2)), vertex.label=NA)
# grouping
set.seed(5)
plot(multi,g.sparrow,vertex.label.color="black", vertex.size=14,
vertex.label.size=1, vertex.label = NA)
par(mfrow=c(1, 3))
set.seed(2024)
plot(g.sparrow, vertex.color=membership(fast), main="Fastgreedy", sub=paste("Modularity:", round(modularity(fast), 2)), vertex.label=NA)
set.seed(2024)
plot(g.sparrow, vertex.color=membership(gv), main="Edge Betweenness", sub=paste("Modularity:", round(modularity(gv), 2)), vertex.label=NA)
set.seed(2024)
plot(g.sparrow, vertex.color=membership(multi), main="Louvain", sub=paste("Modularity:", round(modularity(multi), 2)), vertex.label=NA)