Networks in R
Kelsey Andersen, Robin Choudhury, Karen Garrett
What is a network? Some key terms
• Nodes
• Edges
• Dyads – two nodes
• Triads – three nodes
Node
Link
Also known
as a vert e x
Also known as
an edge
Applications of Networks in Plant
Pathology
• Social networks – networks of connection between individuals
(researchers, producers, land managers)
• Links in this case could represent exchange of goods, information, seed, etc.
• Trade networks – networks of plant movement locally and globally
pose major phytosanitary risks
• Networks of association of microbes – viromes, microbiomes, etc.
• Gene co-expression networks
Networks 101: A network is basically
an adjacency matrix
To
N1 N2 N3
From
Node1 0 1 1
Node2 0 0 0
Node3 1 0 0
Can you draw what this network would look
like?
To
N1 N2 N3
From
N1 0 0 0
N2 1 0 1
N3 0 0 0
Directed or undirected network?
To
N1 N2 N3
From
N1 0 1 0
N2 0 0 1
N3 0 1 0
To
N1 N2 N3
From
N1 0 1 0
N2 1 0 1
N3 0 1 0
Symmetric!
Examples of general questions to ask
with networks
• What nodes and links have particularly important roles in the
network?
• Are there clusters of nodes?
• How “complex” is the network?
• What would strengthen or weaken the network?
Today we will do most of our work in igraph
• igraph reference manual
• http://igraph.org/c/doc/igraph-introduction.html
• Katya Ognyanova has prepared some user friendly introductions
• Network visualization: http://kateto.net/network-visualization
• igraph in R: http://kateto.net/networks-r-igraph
• There are other options, such as ggnetwork, but there is less
documentation available for these packages
Creating networks with igraph
• We can start with a simple adjacency matrix:
Specify number of columns
and rows
Quickly visualize our network using plot()
Network Aesthetics
Some important igraph plotting parameters
Nodes
vertex.color: color of node
vertex.frame.color: border color of node
vertex.shape: shape of node
vertex.label.family: font type
vertex.label.dist: distance of label from node
vertex.label.cex: font size of label
Edges
edge.color: color of edge
edge.width: size of edge
edge.arrow.size: arrow size (for directed networks)
edge.curved: curved edge
Adjusting labels
Curving and coloring edges
Changing shapes of nodes and edges
Other ways to adjust network aesthetics
V = Vertex
E = Edge
Network data types: nodelist and edgelist
• Adjacency matrices can get large quickly
• Another way to read in data is to use an edgelist and a nodelist
Here is our nodelist for a producer network:
First column in “node ID”
Network data types: nodelist and edgelist
“node ID” to “node ID”
Create an igraph object
Edgelist
Nodelist
**All Node ID’s must be
present in the nodelist
Check your
network
Associating colors with node attributes
Color node and
node frame
based on
gender
Adjusting and saving layouts
(Kamada-Kawai layout algorithm)
Sizing nodes based on
attributes
Scale by 2
Size links based on link attributes
Size links based on the
number of times producers
communicated
Fix layout with one of our
previous layouts
Adding a legend
Saving an igraph object