Code
library(igraph)
library(ggplot2)
library(Hmisc)
This is a cheat sheet for the sixth homework on cohesion and community. It works through each step in R.
First, you need the following packages.
library(igraph)
library(ggplot2)
library(Hmisc)
Next, we bring the data into igraph. We have an igraph object stored as a .rds file. The data consist of an advice seeking network within a consulting firm: (“Please indicate how often you have turned to this person for information or advice on work-related topics in the past three months”). 0: I Do Not Know This Person; 1: Never; 2: Seldom; 3: Sometimes; 4: Often; and 5:Very Often.) Not knowing and never seeking advice edges have been dropped. The igraph objects include two attributes - gender (1: male; 2: female), region (1: Europe; 2: USA). You can learn more about the data here.
<- readRDS("data/orgnet.rds")
orgnet
summary(orgnet)
IGRAPH 2fdca3c UNW- 44 320 --
+ attr: name (v/c), region (v/n), gender (v/n), weight (e/n)
<- layout_with_kk(orgnet)
layout.kk
plot(orgnet, layout=layout.kk, vertex.color=V(orgnet)$region, edge.arrow.size=.1)
Let’s start looking at some basic aspects of cohesion.
Here, we identify the largest bicomponet and then plot by region and gender.
<- biconnected.components(orgnet)
bicomponent.org
<- bicomponent.org$components[[max(length(bicomponent.org$components))]]
b1
<- induced.subgraph(graph=orgnet,vids=b1)
bi.g
plot.igraph(bi.g, vertex.color=V(orgnet)$region, main="Consulting Network Bicomponent (Region)")
plot.igraph(bi.g, vertex.color=V(orgnet)$gender, main="Consulting Network Bicomponent (Gender)")
Next we identify the largest cliques (note that there may be several cliques of this size).
<- largest_cliques(orgnet)
cliques
<- cliques[[max(length(cliques))]]
c1
<- induced.subgraph(graph=orgnet,vids=c1)
clique2
plot(clique2, vertex.color=V(orgnet)$region, edge.arrow.size=.1, main="Consultancy Clique (Region)")
plot(clique2, vertex.color=V(orgnet)$gender, edge.arrow.size=.1, main="Consultancy Clique (Gender)")
We can use table to simply find a distribution of cores.
<- coreness(orgnet)
kcore
table(kcore)
kcore
1 5 6 7 9 10 11
1 1 2 3 16 1 20
<- articulation.points(orgnet)
art_points
<- delete.vertices(orgnet, art_points)
rem
plot(rem)
We didn’t need to plot the graphs to answer the questions in part 2, but as I already did this, we can just repeat.
plot(clique2, vertex.color=V(orgnet)$gender, edge.arrow.size=.1, main="Consultancy Clique (Gender)")
Now we turn to some community detection techniques on the largest bicomponent.
Can’t help but check really quickly, but next I will just make a data frame.
<- cluster_louvain(bi.g)
lv_comms
V(bi.g)$lv_comms <- lv_comms$membership
<- layout_with_fr(bi.g)
layout.big
plot.igraph(bi.g, layout=layout.big, vertex.color=V(bi.g)$lv_comms)
OK. Let’s just look construct modularity and store for now.
<- data.frame(louvain=
mod.df modularity(bi.g, cluster_louvain(bi.g)$membership),
walktrap=modularity(bi.g, cluster_walktrap(bi.g)$membership),
leiden_large=modularity(bi.g, cluster_leiden(bi.g, resolution_parameter = 2)$membership),
leiden_small=modularity(bi.g, cluster_leiden(bi.g, resolution_parameter = .5)$membership))
::kable(mod.df) knitr
louvain | walktrap | leiden_large | leiden_small |
---|---|---|---|
0.3262153 | 0.3262153 | 0.2529505 | 0.3114553 |
We could use either Louvain or Walktrap. I’ll try Walktrap since I haven’t already constructed it although given the score they are very likely to be identical.
V(bi.g)$walk <- cluster_walktrap(bi.g)$membership
<- c("circle", "square")
shape
V(bi.g)$shape <- shape[as.factor(V(bi.g)$region)]
plot.igraph(bi.g, layout=layout.big, vertex.color=V(bi.g)$walk, vertex.shape=
V(bi.g)$shape, main="Consulting Bicomponent (Walktrap Communities)")
legend("bottomright",
c("Europe", "USA"),
pch=c(1,0) , bty = "n",pt.cex = 2, cex = 1)