Back to Understanding Dark Networks website

#################################################
# What: Creating a two-mode graph
# File: twomode.R
# Created: 09.28.15
# Revised: 09.28.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 data directory
setwd("~/Dropbox/Casting More Light (Book)/Data/Noordin Top/Pajek Files")

Next, load the libraries to use

# Load statnet library
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.
## 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.
library(network)

Load the two-mode Noordin meetings data from a Pajek network file

# Read in the Pajek network file
meetings.net <- as.network(read.paj("Noordin 139 - Meetings.net"), bipartite=TRUE, directed=FALSE)
meetings.matrix <- as.matrix(meetings.net)

We can read in the network data from a csv file too. One of the advantages of doing so is that Pajek files are read as dichotomized networks (i.e., tie values are either 0 or 1), while csv matrices are read with the values of the ties are retained. That doesn’t matter here since we only have “0s” and “1s”, but it is still helpful to know how to read network data from csv files.

# Read CSV file and convert to network file
meetings.mat <- as.matrix(read.csv(("meetings.csv"),header=TRUE,row.names=1,check.names=FALSE))
meetings.net <- as.network.matrix(meetings.mat,directed=FALSE,ignore.eval=FALSE)

Try an initial plot

# Plot (hide isolates)
gplot(meetings.net,gmode="twomode",displayisolates = FALSE,usearrows=FALSE,
      label=network.vertex.names(meetings.net),label.col="black",label.cex=0.7,label.pos=5)

Let’s change the colors (gray scale) and then plot the network again

# Change colors
m <- matrix(data="dark gray",ncol=139) # Assign the actors to gray color
n <- matrix(data="white",ncol=20) # Assign the meeting to a white color
color <- c(m,n) # combine into a single vector

# Plot again
gplot(meetings.net,gmode="twomode",displayisolates = FALSE,usearrows=FALSE,vertex.col=color,
      label=network.vertex.names(meetings.net),label.col="black",label.cex=0.7,label.pos=5)

# Plot with larger nodes
gplot(meetings.net,gmode="twomode",displayisolates = FALSE,usearrows=FALSE,vertex.col=color,
      label=network.vertex.names(meetings.net),label.col="black",label.cex=0.7,label.pos=5,
      vertex.cex = 2)

We can adjust where the labels are placed. 0 results in labels which are placed away from the center of the plotting region; 1, 2, 3, and 4 result in labels being placed below, to the left of, above, and to the right of vertices (respectively); and label.pos>=5 results in labels which are plotted with no offset (i.e., at the vertex positions). 0 is the default

gplot(meetings.net,gmode="twomode",displayisolates = FALSE,usearrows=FALSE,vertex.col=color,
      label=network.vertex.names(meetings.net),label.col="black",label.cex=0.7,label.pos=0)

One convenient function is that you can save the coordinates of a graph and then use them in subsequent graphs. In other words, you can plot and replot a network until you find a layout you like (e.g., if you want a certain cluster to be placed on the left side of the graph, not the right). To do so, use the following command.

coord <- gplot(meetings.net,gmode="twomode",displayisolates = FALSE,usearrows=FALSE,vertex.col=color,
               mode="kamadakawai",label=network.vertex.names(meetings.net),label.col="black",
               label.cex=0.7,label.pos=5)

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.

# Save plot
tiff(file = "Figure 3.6.tiff",width = 8,height = 8,units = 'in',res = 300)
gplot(meetings.net,gmode="twomode",displayisolates = FALSE,usearrows=FALSE,vertex.col=color,
               label=network.vertex.names(meetings.net),label.col="black",
               label.cex=0.6,coord=coord)
dev.off()
## quartz_off_screen 
##                 2
jpeg(file = "Figure 3.6.jpeg",width = 8,height = 8,units = 'in',res = 300)
gplot(meetings.net,gmode="twomode",displayisolates = FALSE,usearrows=FALSE,vertex.col=color,
      label=network.vertex.names(meetings.net),label.col="black",
      label.cex=0.6,coord=coord)
dev.off()
## quartz_off_screen 
##                 2

That’s all for now

Back to Understanding Dark Networks website