4 Complex Networks

To complete these assignments you need these packages:

library(igraph)
library(qgraph)
library(casnet) 
  • There are many frameworks for analysing and plotting graphs and they are implemented on different platforms often in more-or-less the same way. We’ll use the igraph framework: https://igraph.org for R, but there many more options, see e.g. https://github.com/briatte/awesome-network-analysis#r
  • The manual pages of package igraph in R provide a lot of information, but not everything the package can do. One thing that is missing are the parameters available to customise the graphs for plotting. If you can’t find something in the R manual, have a look at the online documentation: https://igraph.org/r/doc/plot.common.html
  • Package qgraph was developped to fit and analyse the so-called Gaussian Graph Model, e.g in the symptom network studies. It is built on the igraph framework and you can transform qgraph output to an igraph object using the function qgraph::as.igraph.qgraph()
  • The functions in package casnet developped to create, analyse and plot recurrencde networks are also based on the igraph framework.

4.1 Creating, plotting and describing simple graphs

We’ll start using igraph and create some simple graphs.

To create a small ring graph and plot it run the code below.

gR <- graph.ring(20)

# This will display the igraph structure
gR
## IGRAPH 35bfcbe U--- 20 20 -- Ring graph
## + attr: name (g/c), mutual (g/l), circular (g/l)
## + edges from 35bfcbe:
##  [1]  1-- 2  2-- 3  3-- 4  4-- 5  5-- 6  6-- 7  7-- 8  8-- 9  9--10 10--11
## [11] 11--12 12--13 13--14 14--15 15--16 16--17 17--18 18--19 19--20  1--20
# To plot, simply call:
plot(gR)

You can request to represent the graph as a so-called edge-list which just lists the edges in a graph by reporting the nodes the edge connects from - to

get.edgelist(gR)
##       [,1] [,2]
##  [1,]    1    2
##  [2,]    2    3
##  [3,]    3    4
##  [4,]    4    5
##  [5,]    5    6
##  [6,]    6    7
##  [7,]    7    8
##  [8,]    8    9
##  [9,]    9   10
## [10,]   10   11
## [11,]   11   12
## [12,]   12   13
## [13,]   13   14
## [14,]   14   15
## [15,]   15   16
## [16,]   16   17
## [17,]   17   18
## [18,]   18   19
## [19,]   19   20
## [20,]    1   20

Other useful functions are E() and V() for manipulating attributes of Edges (connections) and Vertices (nodes). See https://igraph.org/r/doc/plot.common.html for all available attributes.

# List vertices
V(gR) 
## + 20/20 vertices, from 35bfcbe:
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
# List edges
E(gR)
## + 20/20 edges from 35bfcbe:
##  [1]  1-- 2  2-- 3  3-- 4  4-- 5  5-- 6  6-- 7  7-- 8  8-- 9  9--10 10--11
## [11] 11--12 12--13 13--14 14--15 15--16 16--17 17--18 18--19 19--20  1--20
# Set vertex color
V(gR)$color <- "red3"
# set vertex label
V(gR)$label.color <- "white"
# Set vertex shape
V(gR)$shape <- "square"
# Set edge color
E(gR)$color <- "steelblue"
# Set efge size
E(gR)$width <- 4

# Plot
plot(gR)