Back to Understanding Dark Networks website

It’s generally helpful to include some sort of header at the top of your scripts that include basic information about the script, such as it’s name, purpose, etc. When you place a “#” sign at the beginning of the line, R treats it as a comment rather than as a command.

#################################################
# What: hypothetical graph
# File: hypothetical.R
# Created: 09.21.15
# Revised: 09.21.15
#################################################

Clearing your workspace before starting is generally recommended

# Clear workspace
rm(list=ls())

It is usually a good idea to set your working directory to where your data are located. You will obviously have to set it to your own directory, not the one listed below.

# Set working directory
setwd("~/Dropbox/Casting More Light (Book)/Chapters/1. Social Networks and Dark Networks")

Let’s get started. First load the R libraries you intend to use. Here we will use the “igraph” package to create and plot our hypothetical graph.

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

Rather than read in already prepared network data, here we will generate our own network – in particular a scale-free network (see Barabasi 2002).

# Generate scale-free network
hypothetical.igraph <- sample_pa(23,directed=FALSE)

Now, let’s try a few plots using different layout algorithms. These won’t generate exactly the same graph as the one in the first chapter, but they will be similar. The first uses the Kamada-Kawai algorithm. The second, Fruchterman-Reingold. The third, multi-dimensional scaling. Note that the coordinates from the layout are saved, which can then be reused (if one likes the layout).

# Explore a few plots
coordkk <- plot(hypothetical.igraph,layout=layout.kamada.kawai,vertex.color="gray",
                vertex.size=10,vertex.label.cex=.8,vertex.label.color="black")

coordfr <- plot(hypothetical.igraph,layout=layout.fruchterman.reingold,vertex.color="gray",
                vertex.size=10,vertex.label.cex=.8,vertex.label.color="black")

coordmds <- plot(hypothetical.igraph,layout=layout.mds,vertex.color="gray",
                 vertex.label.cex=.8,vertex.label.color="black")

Saving plots is relatively easy. However, if you want graphs with high resolution, then you will you need to include additional commands such as those listed below, which indicates the size of the graphs, as well as their resolution. Note that we used the coordinates from the graphs for the layouts of the graphs below. This is a useful technique for replicating layouts that you like.

# Save the above plots as TIFF files
tiff(file = "hypotheticalkk.tiff",width = 6,height = 6,units = 'in',res = 300)
plot(hypothetical.igraph,layout=coordkk,vertex.color="gray",vertex.size=10,
     vertex.label.cex=.8,vertex.label.color="black")
dev.off()
## quartz_off_screen 
##                 2
tiff(file = "hypotheticalfr.tiff",width = 6,height = 6,units = 'in',res = 300)
plot(hypothetical.igraph,layout=coordkk,vertex.color="gray",vertex.size=10,
     vertex.label.cex=.8,vertex.label.color="black")
dev.off()
## quartz_off_screen 
##                 2
tiff(file = "hypotheticalmds.tiff",width = 6,height = 6,units = 'in',res = 300)
plot(hypothetical.igraph,layout=coordmds,vertex.color="gray",vertex.size=10,
     vertex.label.cex=.8,vertex.label.color="black")
dev.off()
## quartz_off_screen 
##                 2

That’s all for now

Back to Understanding Dark Networks website