Fast paced food-web plotting action

December 20, 2011
By

(This article was first published on distributed ecology, and kindly contributed to R-bloggers)

A simple foodweb
One of my interests is in food web topology and food web dynamics. My research in experimental ponds has left me with 2430 food webs to make sense of, and one way to facilitate understanding that many networks, and really any food web, is through visualization. There are fancy ways to visualize large webs such as FoodWeb3D, but A). I usually don’t have huge webs, and B). I’ve become a died in the wool ggplot2 convert, so I wanted to be able to create plots using ggplot2. What I needed was a function that would return a ggplot2 object that was a barebones plot of my web. Before I get into the details of the plotting function, it’s worth a few moments to go over what a food web diagram looks like and what it visualizes.  Each point is a species and each line is saying that one species eats the other.  The diagram on the right is a visualization of an S x S square matrix containing 1’s and 0’s where S is the number of species and a 1 in a column means that the species in column number x eats the species in row number y.  The central task is how do I take that square matrix and convert it to ordered points and then plot it? The first job is simply to create a set of nodes, and to do this I wrote a function where the input was the number of points you want, and it returns that number evenly spaced along the unit circle.

########## support function create.xy
########## returns regularly spaced circular coordinates for the size
########## of your web

create.xy <- function(po){
degs <- seq(0,2*pi,by=(2*pi/(po)))
return(cbind(cos(degs),sin(degs)))
}

Network diagram with color!
The actual plotting function works by looping through each column of the matrix and determining which points have 1’s (indicating consumption) and then repeating xy coordinates for the consuming node (species) in a matrix, and then creating a matrix of xy coordinates of the nodes consumed. A tricky part is that these two matrices need to be combined into a single data frame and properly ordered. I achieve this by indexing each point in the consumer matrix with an odd number, and each point in the consumed species matrix with an even number. Then after combining the two different matrices, I can order them by the index and convert it to a data frame object so ggplot2 can plot them. On top of the matrix of 1’s and 0’s the function requires a series of color labels for each consumer node. It can be all black, but it allows for plots with different colors such as the one at right. I’ve used the convention of red for predator links and green for herbivory links, but you could use whatever colors.  One problem is that if there are lots of reciprocal links (Species A eats B, but B also eats A) and each has their own color, then the colors will mix in the figure (red and blue become purple in the above case), so be wary of using too many colors.
A time series of food webs
  I also stripped each plot of any labels and axes, this is easy enough to modify in the code where I call ggplot. Other things like adding line weights for interactions strengths, arrows for directionality, etc can all be modified within the ggplot options.  One of the most flexible parts for me was that the function returns a ggplot2 object which I can then use to create series of plots.  For instance I’m looking at food web dynamics within my ponds so like to visualize a time series of webs.  Using the arrange() function with a list of ggplot2 objects I can easily do this. The function will also return the raw data frame that can be used for any other plotting you might want to do. You can see a Gist of all the code here, or add to it on the github page. Here are some more examples of plots below. Note that as the number of species increases the webs become impossible to discern, such as the 249 species Caribbean food-web



20 species niche model249 species web, Bascompte et al 2005 40 species random web

To leave a comment for the author, please follow the link and comment on his blog: distributed ecology.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.