(This article was first published on

**distributed ecology**, and kindly contributed to R-bloggers)A simple foodweb |

*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! |

*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 |

20 species niche model | 249 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...