Plotting Networks with igraph

Official website:

http://igraph.org/r/

How I’ve used it for visualising the microbial groups in my microPop R package:

https://helenkett.shinyapps.io/MicrobialNetworks/

Example

Simplified Microbial Network for Methane Production

Fermenting bacteria (‘Fermenters’) consume carbohydrates and convert them into bacterial growth, carbon dioxide, hydrogen and other products (e.g. volatile fatty acids).

Methane producing archea (‘Methanogens’) use the hydrogen and the carbon dioxide to grow, producing methane and water in the process.

This process is typically found in the rumen (e.g. in cows, goats, horses, sheep etc ) and sometimes in the human gut.

Define Nodes

nodes=cbind('id'=c('Fermenters','Methanogens','carbs','CO2','H2','other','CH4','H2O'),
            'type'=c(rep('Microbe',2),rep('nonBio',6)))
nodes
##      id            type     
## [1,] "Fermenters"  "Microbe"
## [2,] "Methanogens" "Microbe"
## [3,] "carbs"       "nonBio" 
## [4,] "CO2"         "nonBio" 
## [5,] "H2"          "nonBio" 
## [6,] "other"       "nonBio" 
## [7,] "CH4"         "nonBio" 
## [8,] "H2O"         "nonBio"

Make the network

library(igraph)
net = graph_from_data_frame(links,vertices = nodes,directed = T)
plot(net)

Change Appearance

colrs.v = c(nonBio = "lightblue",Microbe = "gold") #node colours
V(net)$color = colrs.v[V(net)$type]

colrs.e = c(output = "grey", uptake = "magenta") #edge colours
E(net)$color = colrs.e[E(net)$type] 

plot(net, edge.curved=0.2,vertex.size=30) #make nodes bigger, curve arrows

Some difficulties I had with igraph

1/ The graphic shown in R (or Rstudio) is not the same as that which is shown in R markdown or R shiny.

2/ Vertices too close together

Solutions:

To increase space between vertices see: https://stackoverflow.com/questions/38999656/increasing-spaces-between-vertices-for-r-igraph

You can also specify exactly where you want the nodes: https://stackoverflow.com/questions/11272349/igraph-axes-xlim-ylim-plot-incorrectly

3/ Too much white space in the plot