For this portion of the workshop, we will use the following packages:

Networks with igraph

Simple Networks

First, let’s create a simple adjacency matrix with three rows and three columns:

##      [,1] [,2] [,3]
## [1,]    0    0    1
## [2,]    1    0    0
## [3,]    0    1    0

Use the igraph function graph_from_adjacency_matrix() to create a network object from your graph, then use the plot() function to plot. Use edge.arrow.size to set the size of the arrows.

 

Alternatively, create the same network by telling igraph what links you would like and how many nodes you would like to establish. We also want to tell igraph if our network is directed (with arrows) or undirected.

Network Aesthetics

Many parts of a network can be sized and colored to help communicate results more clearly.
Here, for example, we color the nodes and change the size and position of the labels using vertex.color = and vertex.label.dist =

Labels

Here we use vertex.label.cex to make our labels slightly bigger, and we use vertex.label.dist to move the labels off the network. This can be useful, particularly when we have large networks.

Colors

Here we color the node borders the same color as the nodes themselves, but this could be modified to a different color that gives additional information about the node:

We can also change edge colors. This can be useful when edges mean different things.

Other ways to change aesthetics

We can also adjust plot aesthetics by applying changes directly to the verticies V() or edges E() of our plots.

Network data types

Adjacency matrices vs node lists

  You can read in your data directly as an adjacency matrix, but likely this is not the way that you have your data organized. Instead, it might be easier to have two files: a node file and an edge file.

In a node file, the first two columns are all of your from:to links. Column 1 is always from, Column 2 is always to (less important for undirected networks). The columns after that are your edge attributes (such as weight of link, volume, probability, name etc).
 

Here is an example of a simple node list, where all of the nodes are farmers and links are communication. We include attributes about the node like age, gender, and number of years farming.

##       Names YearsFarming Age Gender
## 1       Jim          8.5  27   Male
## 2    Carole          6.5  52 Female
## 3       Joe          4.0  49   Male
## 4  Michelle          1.0  32 Female
## 5       Jen          3.0  65 Female
## 6      Pete         10.0  72   Male
## 7      Paul          5.0  42   Male
## 8       Tim          5.0  67   Male
## 9      Jess          5.0  48 Female
## 10     Mark          1.0  33   Male
## 11     Jill          1.0  67 Female
## 12      Cam          6.0  75   Male
## 13     Kate          6.0  39 Female

 

Now we can create an edgelist data frame- Who shared information in the 2019 growing season? How frequently?

##    From     To Times
## 1   Jim Carole     3
## 2   Jim    Jen     7
## 3   Jim   Pete     6
## 4  Jill Carole     6
## 5  Kate    Joe     5
## 6  Pete Carole     3
## 7  Pete   Paul     2
## 8  Jess   Mark     1
## 9   Jim    Cam     1
## 10  Jim   Mark     2
## 11 Pete    Tim     5

igraph objects

Let’s make our farmer communication network!

## IGRAPH f7ba75b DN-- 13 11 -- 
## + attr: name (v/c), YearsFarming (v/n), Age (v/n), Gender (v/c), Times
## | (e/n)
## + edges from f7ba75b (vertex names):
##  [1] Jim ->Carole Jim ->Jen    Jim ->Pete   Jill->Carole Kate->Joe   
##  [6] Pete->Carole Pete->Paul   Jess->Mark   Jim ->Cam    Jim ->Mark  
## [11] Pete->Tim
## + 11/11 edges from f7ba75b (vertex names):
##  [1] Jim ->Carole Jim ->Jen    Jim ->Pete   Jill->Carole Kate->Joe   
##  [6] Pete->Carole Pete->Paul   Jess->Mark   Jim ->Cam    Jim ->Mark  
## [11] Pete->Tim
## + 13/13 vertices, named, from f7ba75b:
##  [1] Jim      Carole   Joe      Michelle Jen      Pete     Paul     Tim     
##  [9] Jess     Mark     Jill     Cam      Kate

Plot!

Node size

Scale the node size up a bit..

We can also adjust the size of the links. Here we will size them based on the number of times any two farmers communicated.