Food web for Otago Harbor, NZ:
One of the many things I have been trying to figure out is how to plot food webs in a manner that makes sense, using the igraph package in R, see here. I finally understand the code required to lay out the web in a sensible manner, that is by trophic level. It turns out to be a relatively simple process. Rather than using one of the provided layouts; circle, kamada kawai, fruchterman reingold, etc, you can set the coordinates of each node.
Thus, if you set the y coordinate to trophic level, and set the x coordinate to a value randomly drawn from a uniform distribution you get the above layout. The easiest way to get trophic levels is with the NetIndices package. TrophInd() is the function, it takes an adjacency matrix as an input and the output is the trophic level of each node, along with an index of omnivory.
library(igraph) library(NetIndices) lay<-matrix(nrow=123,ncol=2) # create a matrix with one column as runif, the other as trophic level lay[,1]<-runif(123) lay[,2]<-TrophInd(predweb.adj[])$TL-1 par(mar=c(.1,.1,.1,.1)) plot.igraph(otagopred,layout=lay,vertex.label=NA,vertex.size=2,edge.arrow.size=.5,edge.width=.5)
Here “otagopred” is the graph object for the above plot, “predweb.adj[]” is a list of several adjacency matrices for a set of food web data that I have.
I was also able to do some neat stuff with this web, like coloring some of the parasite links differently.
library(igraph) library(NetIndices) lay.o<-matrix(c(runif(180,0,10),TrophInd(paraweb.adj[])$TL),ncol=2) E(otagoedge)$color<-"grey" E(otagoedge)[wotagolink[,3]==1]$color<-"grey" E(otagoedge)[wotagolink[,3]==6]$color<-"blue" #macroparasite E(otagoedge)[wotagolink[,3]==9]$color<-"black"#commensal E(otagoedge)[wotagolink[,3]==12]$color<-"red" #trophic transmitted parasite V(otagoedge)$color="skyblue" vert.macro<-wotagolink[,2][wotagolink[,3]==6] V(otagoedge)[vert.macro]$color<-"darkblue" par(mar=c(.1,.1,.1,.1)) plot.igraph(otagoedge,layout=lay.o,vertex.label=NA,vertex.size=2,edge.arrow.size=.5,edge.width=.5)</p>
Another way to plot webs is with the relatively new cheddar package (I am planning a more detailed post about it to come soon). There are a few functions for plotting webs using the cheddar package, but my favorite is PlotWebByLevel(). One nice thing about cheddar is the fact that the functions are named quite nicely.
library(cheddar) data(TL86) PlotWebByLevel(TL86)
Cheddar also comes with a few fun food web datasets, like this one from Tuesday Lake circa 1986 (they also have 1984).