More food web plotting with R

Food web for Otago Harbor, NZ:otagopredweb1

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[[1]])$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[[1]]” 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.

otagoparaweb2Where blue links are macroparasitic interactions, red are trophically transmitted parasites, and black is commensal interactions.


library(igraph)
library(NetIndices)
lay.o<-matrix(c(runif(180,0,10),TrophInd(paraweb.adj[[1]])$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)

tl86web

Nice simple and clean. It even provides a title for you.

Cheddar also comes with a few fun food web datasets, like this one from Tuesday Lake circa 1986 (they also have 1984).

Advertisements
This entry was posted in Research and tagged , , . Bookmark the permalink.

2 Responses to More food web plotting with R

  1. Pingback: Oikos, food web methods, and PARASITES! | Assembling my Network

  2. Pingback: Blogroll: Assembling my Network | Scientific Gems

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s