Back to Understanding Dark Networks website

#######################################################
# What: Shrinking (collapse) subnetworks (Figures 3.19)
# File: collapse.R
# Created: 07.11.15
# Revised: 09.30.15
#######################################################

Clear workspace

## Clear workspace
rm(list=ls())

Set working directory to where data are located.

# Set data directory
setwd("~/Dropbox/Casting More Light (Book)/Data/Noordin Top/Pajek Files")

Load igraph library because it allows us to shrink (collapse) a network

# Load statnet libraries
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union

Read in network and attribute data. Network data are in Pajek format, while the attribute data are in a csv file.

# Read in network file and transform it to undirected graph
combined.ig <- read.graph("Combined Network (Aggregated).net",format = c("pajek"))
combined.ig <- as.undirected(combined.ig,edge.attr.comb=list(weight="mean"))

# Read in attribute data and create role vector
attributes.matrix <- as.matrix(read.csv(("attributes.csv"),header=TRUE,row.names=1,check.names=FALSE))
logistics.vec <- attributes.matrix[,10]

There are “0s” in the role vector and igraph doesn’t like them when it comes to shrinking networks, so we will add “1” to each value in the vector. This, of course, means that the values won’t agree with the codebook but will be off by one.

# Add one to the role vector
logistics.vec <- logistics.vec + 1
# Shrink (Collapse) by role
logistics.ig <- contract.vertices(combined.ig,logistics.vec)

Initial plot of collapsed network

# Plot
plot(logistics.ig)

Calculate betweenness centrality and vary node size by betweenness

# Calculate betweenness centrality and then vary node size by betweenness
bet.vec <- betweenness(logistics.ig)
plot(logistics.ig,layout=layout.fruchterman.reingold,vertex.size=bet.vec)

# Unknown logistical function node is too large relative to other nodes, so reduce size by dividing
# vector, and adding "5" to minimum size so that nodes without betweenness centrality can still be seen

plot(logistics.ig,layout=layout.fruchterman.reingold,vertex.size=((bet.vec/2)+5))

Simplify the network (reduce to a single line), add labels, change color of vertices & labels, and change font size of labels

# Simplify 
logistics.ig <- simplify(logistics.ig)

# Add labels
logistics.ig <- set.vertex.attribute(logistics.ig, "vertex.names",value=c("None or Unclear","Safe House","Weapons",
                                      "Transportation","Material","Weapons, Transportation, Material",
                                      "Weapons, Material","Transportation, Material","Safehouse, Transportation",
                                      "Safehouse, Transportation, Material","Safehouse, Weapons, Material"))
V(logistics.ig)$vertex.names
##  [1] "None or Unclear"                    
##  [2] "Safe House"                         
##  [3] "Weapons"                            
##  [4] "Transportation"                     
##  [5] "Material"                           
##  [6] "Weapons, Transportation, Material"  
##  [7] "Weapons, Material"                  
##  [8] "Transportation, Material"           
##  [9] "Safehouse, Transportation"          
## [10] "Safehouse, Transportation, Material"
## [11] "Safehouse, Weapons, Material"
V(logistics.ig)$label = V(logistics.ig)$vertex.names

# Plot again (change color of vertices to gray & labels to black - also change font size of labels)
plot(logistics.ig,layout=layout.fruchterman.reingold,vertex.size=((bet.vec/2)+5),vertex.color="gray",
     vertex.label.cex=.6,vertex.label.color="black")

Figure 3.19 in the book was actually created in the statnet (sna) library, so let’s go back to the sna library and visualize the network in it. This requires detaching the igraph library and loading the sna library, as well as converting the igraph object to a statnet (network) object before plotting

# Load and detach libraries
library(sna)
## Loading required package: statnet.common
## Loading required package: network
## network: Classes for Relational Data
## Version 1.13.0 created on 2015-08-31.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##                     Mark S. Handcock, University of California -- Los Angeles
##                     David R. Hunter, Penn State University
##                     Martina Morris, University of Washington
##                     Skye Bender-deMoll, University of Washington
##  For citation information, type citation("network").
##  Type help("network-package") to get started.
## 
## Attaching package: 'network'
## The following objects are masked from 'package:igraph':
## 
##     %c%, %s%, add.edges, add.vertices, delete.edges,
##     delete.vertices, get.edge.attribute, get.edges,
##     get.vertex.attribute, is.bipartite, is.directed,
##     list.edge.attributes, list.vertex.attributes,
##     set.edge.attribute, set.vertex.attribute
## sna: Tools for Social Network Analysis
## Version 2.4 created on 2016-07-23.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##  For citation information, type citation("sna").
##  Type help(package="sna") to get started.
## 
## Attaching package: 'sna'
## The following objects are masked from 'package:igraph':
## 
##     betweenness, bonpow, closeness, components, degree,
##     dyad.census, evcent, hierarchy, is.connected, neighborhood,
##     triad.census
library(intergraph)
detach("package:igraph",unload=TRUE)

# Convert to a network object
logistics.net <- asNetwork(logistics.ig)

# Plot
gplot(logistics.net,label=network.vertex.names(logistics.net),vertex.col="dark gray",label.col="black",
      label.cex=0.7,usearrows=FALSE,label.pos=5,gmode="graph",vertex.cex=(bet.vec/10+.75))

Back to Understanding Dark Networks website