Back to Understanding Dark Networks website
#################################################
# What: Extracting subnetworks (Figures 3.17-3.18)
# File: extracting.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 libraries
# Load statnet libraries
library(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.
library(sna)
## Loading required package: statnet.common
## 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.
Read in network and attribute data. Network data are in Pajek format, while the attribute data are in a csv file. Note that after loading the attribute file, we then extract attribute data about members logistical role (column 10) and current status (column 5). We also assign the current status attribute to each vector.
# Read in network files
combined.net <- as.network(read.paj("Combined Network (Aggregated).net"),directed=FALSE)
# Attributes to vector
attributes.matrix <- as.matrix(read.csv(("attributes.csv"),header=TRUE,row.names=1,check.names=FALSE))
logisticsatt.vec <- attributes.matrix[,10]
currentstatus.vec <- attributes.matrix[,5]
# Assign current status as vertex (actor) attribute
set.vertex.attribute(combined.net,'status',attributes.matrix[,5])
Initial plot (and save coordinates)
# Plot
ccoordkk <- gplot(combined.net,vertex.col="dark gray",mode="kamadakawai",usearrows=FALSE,
label.pos=5,gmode="graph")
ccoordfg <- gplot(combined.net,vertex.col="dark gray",mode="fruchtermanreingold",usearrows=FALSE,
label.pos=5,gmode="graph")
Change the color of the logistical function attribute to gray scale (for available colors, see http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf)
# Change colors of logisticsatt.vec & currentstatus.vec
logisticsatt.vec[logisticsatt.vec == 0] <- "white"
logisticsatt.vec[logisticsatt.vec == 1] <- "gray90"
logisticsatt.vec[logisticsatt.vec == 2] <- "gray80"
logisticsatt.vec[logisticsatt.vec == 3] <- "gray70"
logisticsatt.vec[logisticsatt.vec == 4] <- "gray60"
logisticsatt.vec[logisticsatt.vec == 5] <- "gray50"
logisticsatt.vec[logisticsatt.vec == 6] <- "gray40"
logisticsatt.vec[logisticsatt.vec == 7] <- "gray30"
logisticsatt.vec[logisticsatt.vec == 8] <- "gray20"
logisticsatt.vec[logisticsatt.vec == 9] <- "gray90"
logisticsatt.vec[logisticsatt.vec == 10] <- "black"
currentstatus.vec[currentstatus.vec == 0] <- "black"
currentstatus.vec[currentstatus.vec == 1] <- "gray75"
currentstatus.vec[currentstatus.vec == 2] <- "white"
Plot using colors (gray scale) to indicate logistical function or current status (alive, free, dead)
# Plot and color nodes by logistics function
gplot(combined.net,vertex.col=logisticsatt.vec,usearrows=FALSE,label.pos=5,gmode="graph",coord=ccoordkk)
gplot(combined.net,vertex.col=logisticsatt.vec,usearrows=FALSE,label.pos=5,gmode="graph",coord=ccoordfg)
jpeg(file = "Figure 3.17a.jpeg",width = 8,height = 8,units = 'in',res = 300)
gplot(combined.net,vertex.col=logisticsatt.vec,usearrows=FALSE,label.pos=5,gmode="graph",coord=ccoordfg)
dev.off()
## quartz_off_screen
## 2
# Plot and color nodes by current status
gplot(combined.net,vertex.col=currentstatus.vec,usearrows=FALSE,label.pos=5,gmode="graph",coord=ccoordkk)
gplot(combined.net,vertex.col=currentstatus.vec,usearrows=FALSE,label.pos=5,gmode="graph",coord=ccoordfg)
jpeg(file = "Figure 3.17b.jpeg",width = 8,height = 8,units = 'in',res = 300)
gplot(combined.net,vertex.col=currentstatus.vec,usearrows=FALSE,label.pos=5,gmode="graph",coord=ccoordfg)
dev.off()
## quartz_off_screen
## 2
Extract the Alive and Free network with the induced subgraph command
# Extract the alive and free subset from the network
aliveandfree.net <- get.inducedSubgraph(combined.net,v=which(combined.net%v%'status'=='1'))
Plot and save the Alive and Free network
# Plot
afcoordkk <- gplot(aliveandfree.net,label=network.vertex.names(aliveandfree.net),vertex.col="dark gray",edge.col="gray",
mode="kamadakawai",label.col="black",label.cex=0.6,usearrows=FALSE,label.pos=5,gmode="graph")
afcoordfg <- gplot(aliveandfree.net,label=network.vertex.names(aliveandfree.net),vertex.col="dark gray",edge.col="gray",
label.col="black",label.cex=0.6,usearrows=FALSE,label.pos=5,gmode="graph")
# Save plot
jpeg(file = "Figure 3.18.jpeg",width = 8,height = 8,units = 'in',res = 300)
afcoordfg <- gplot(aliveandfree.net,label=network.vertex.names(aliveandfree.net),vertex.col="dark gray",edge.col="gray",
label.col="black",label.cex=0.6,usearrows=FALSE,label.pos=5,gmode="graph")
dev.off()
## quartz_off_screen
## 2