Shete Boka, Curaçao

Visualizing stomach contents

Turning frequency tables into polarized stacked plots

Visualizing Gut Content

We recently finished a study on the impacts of urbanization on a species of gecko found in Curaçao. Part of this study involved analyzing and visualing stomach content data. Coauthor Andrew Jones supplied some really neat code that creates interesting visuals of diet data.

The above plot is from our manuscript and instantly brings to mind a warped version of the commonly seen “structure plots” in population genetic studies such as our recent study of Lepidonotothen . I rather like Andy’s idea of displaying these plots in circles and think this is a great compliment to the commonly seen frequency tables. In the above plot it is pretty easy to notice lots of army green hues (corresponding to orthopterans and isopods) in panel B and little to none in panel’s C and D.

Here is a walkthrough of Andy's code


Andy’s code uses two other R packages. Install them and load

library(ggplot2)
library(reshape2)

now set your working directory to save files

setwd("~/Documents/your directory here")


In order to follow along, you will need to go here and download the two gecko example csv that begin with “R tutorial” .


To begin, read in the .csv files.

dietsgeckoproportions <- read.csv("R_tutorial_dietfreq.csv")
dietsgeckologic <- read.csv("R_tutorial_dietlogic.csv")

If you look at the first file “R_tutorial_dietfreq.csv” it is a frequency table of the gut contents we found in these geckos. The second file “R_tutorial_dietlogic.csv” adds the locality, sex, and specimen numbers to this data. Now let’s bind this data together and make this a giant stack of data using the handy melt function:

dietsgeckoproportions1 <- cbind(dietsgeckologic$Site2, dietsgeckologic$Specimen.., dietsgeckoproportions)
colnames(dietsgeckoproportions1)[1] <- "Site"
colnames(dietsgeckoproportions1)[2] <- "Specimen"
dietsgeckoproportions2 <- melt(dietsgeckoproportions1, id.var=c("Site","Specimen"))

That is pretty much all we need to start plotting using ggplot. Let’s quickly define a color palette using topo.colors and 15 colors since we have 15 prey categories.

cols = topo.colors(15)

Note here that you could use cols = rainbow(15, s=.6, v=.9)[sample(1:15,15)] to get a rainbow template or cols = c("#4C00FFFF", "#0000FFFF", "#004CFFFF" ....) to feed a vector of user defined colors.


Now that we have a color scheme let’s make a ggplot object that we can plot

p2 <- ggplot(dietsgeckoproportions2, aes(x = Specimen, y = value, fill = as.factor(variable))) +
  geom_bar(stat = "identity",width=1) + theme_set(theme_bw(10)) + 
  scale_fill_manual(values = cols, name = "Prey Category", 
  breaks=c("Diptera", "Lepidoptera","Coleoptera","Archnid_scorp","Archnid_spider","Other", "Nematodes", "Centipede", "Isopod", "Hemiptera", "Hymenoptera", "Orthoptera", "Ephermeroptera", "Gecko_skin", "Blattaria"), 
  labels=c("Diptera", "Lepidoptera","Coleoptera","Archnid scorpion","Archnid spider","Other", "Nematodes", "Centipede", "Isopod", "Hemiptera", "Hymenoptera", "Orthoptera", "Ephermeroptera", "Gecko skin", "Blattaria"))


It looks like a lot, but the bulk of that block is just definied prey categories. Basically the above is using our melted object to set the conditions of the stacked plot. If you don’t like the polarized look, you can get a stacked plot by simply typing p2 .

The legend will need to be tweaked and it is a little off but you get the idea and can mess with the ggplot settings to customize. Since we are moving on to circular plots we can ignore this and add one last line of code.

p2 + coord_polar() + ylim(-0.5,1.2)

That’s it! From here you can keep modifying the above, breaking out different subsets of data such as sites, or begin to use your favorite graphic editing software to customize as you like.


If you use this tutorial and code in a project, please cite:

2016. Dornburg, A., Smith, C., Federman, S., Moore, J.A., Warren, D.L., Iglesias, T., Brandley, M.C., Watkins-Colwell, G.J., Lamb, A.D., Jones, A. Disentangling the influence of urbanization and invasion on endemic reptiles in tropical biodiversity hotspots: A case study of Phyllodactylus martini along an urban gradient in Curaçao. Accepted. Peabody Bulletin of Natural History.

Dialogue & Discussion