library(asnipe)
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
library(RColorBrewer)
# Creating a graph from the 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)
mat=get_network(t(assoc), association_index="SRI")
## Generating 25 x 25 matrix
g.sparrow=graph_from_adjacency_matrix(mat, "undirected", weighted=T)
plot(g.sparrow, edge.width=E(g.sparrow)$weight, vertex.label="")
Community detection is used to identify cohesiveness between subgroups within a network. It is one of the most widespread strategies in network analysis. In igraph, we can implement community detection algorithms to analyze data. For example, dense subgroups (communities) could lead to network fragmentation despite having quick resource flow within the subgroup. Additionally, a network with very few subgroups could indicate inclusiveness or an opportunity for brokerage. An important thing to consider with these algorithms is that they are not informed by theory, and are just raw computations of the given data. We will observe three community detection algorithms: fastgreedy, Girvan-Newman(edge betweenness), and multilevel.
sprw_fastgreed <- fastgreedy.community(g.sparrow)
dendPlot(sprw_fastgreed)
set.seed(447)
plot(sprw_fastgreed,g.sparrow,vertex.label=NA)
modularity(sprw_fastgreed)
## [1] 0.609246
length(sprw_fastgreed)
## [1] 4
membership(sprw_fastgreed)
## 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
sizes(sprw_fastgreed)
## Community sizes
## 1 2 3 4
## 9 6 5 5
sparrow_gv <- edge.betweenness.community(g.sparrow, modularity = TRUE)
## Warning in edge.betweenness.community(g.sparrow, modularity = TRUE): At
## community.c:461 :Membership vector will be selected based on the lowest
## modularity score.
## Warning in edge.betweenness.community(g.sparrow, modularity = TRUE): At
## community.c:468 :Modularity calculation with weighted edge betweenness community
## detection might not make sense -- modularity treats edge weights as similarities
## while edge betwenness treats them as distances
dendPlot(sparrow_gv, mode="hclust")
set.seed(447)
plot(sparrow_gv,g.sparrow,vertex.label=NA)
modularity(sparrow_gv)
## [1] 0.5731284
length(sparrow_gv)
## [1] 4
membership(sparrow_gv)
## 23820 23726 23831 23763 23772 23770 23771 23777 23774 23860 23779 23773 23862
## 1 1 1 1 2 3 4 4 4 3 4 4 4
## 23857 23871 23853 23732 23734 23756 23759 23768 23758 23781 23815 23809
## 3 3 1 4 2 4 4 4 4 3 3 3
sizes(sparrow_gv)
## Community sizes
## 1 2 3 4
## 5 2 7 11
sparrow_ml <- multilevel.community(g.sparrow)
set.seed(447)
plot(sparrow_ml,g.sparrow,vertex.label=NA)
modularity(sparrow_ml)
## [1] 0.609246
length(sparrow_ml)
## [1] 4
membership(sparrow_ml)
## 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
sizes(sparrow_ml)
## Community sizes
## 1 2 3 4
## 5 9 5 6
par(mfrow=c(1,3))
# Fastgreedy plot
set.seed(447)
colors <- brewer.pal(length(sprw_fastgreed), 'Set1')
V(g.sparrow)$color = colors[membership(sprw_fastgreed)]
plot(g.sparrow,vertex.label=NA,vertex.size=16)
title(main ="Fastgreedy",sub="Modularity = 0.61",line=-3)
# Girvan-Newman plot
set.seed(447)
colors <- brewer.pal(length(sparrow_gv), 'Set1')
V(g.sparrow)$color = colors[membership(sparrow_gv)]
plot(g.sparrow,vertex.label=NA,vertex.size=16)
title(main="Girvan-Newman",sub="Modularity = 0.57",line=-3)
# Multilevel plot
set.seed(447)
colors <- brewer.pal(length(sparrow_ml), 'Set1')
V(g.sparrow)$color = colors[membership(sparrow_ml)]
plot(g.sparrow,vertex.label=NA,vertex.size=16)
title(main="Multilevel",sub="Modularity = 0.61", line=-3)
mtext("Community Detection Algorithms", side = 3, line = -2.8, outer = TRUE, cex = 2)
mtext("NOTE: Colors are randomized across plots. Colors are not assigned to a class,
they only indicate which nodes are inside of a certain group.", side = 3,
line = -33, outer = TRUE, cex = .6)