Incubation Rhythm Figures in R


An incubation rhythm figure summarizes the on and off bouts at a given nest through time and over successive days. This page provides the necessary documentation to make an incubation rhythm figure using the program R in conjunction with the package "ggplot2". Such a figure will look something like this:



The final figure, as currently envisioned, could be improved in a number of significant ways. Most relevantly, I would like to display percent of day spent on the nest along the right-hand Y-axis. GGplot2 doesn't readily support second axes, and H. Wickham is not psyched about them, so he's liable not to incorporate them anytime soon. That being said, even as currently implemented, this is a time-saving tool that produces intelligible results that should be easily comparable between different species.

Follow along carefully with the example xls spreadsheet as you read through these instructions

Start a new excel spreadsheet. Make a separate worksheet for each day of the incubation or brooding period you are watching. Title the worksheet as the day.

In each worksheet, the:
1st column is the start and end time of the observation for the day
2nd column is the time, formatted in 6:00:00 & 18:00:00 style
3rd column is the activity that happens AT that time in the corresponding 2nd column

At the end of all the days, add a worksheet that is a summary of all the days. This final worksheet should have 6 columns. Into this worksheet you are collating information. First, figure out what time you want the figure to start and end at. A good example is 15 minutes before and after the earliest and latest times you recorded, e.g. if all your videos start after 6 AM and end before 18:30:00, start at 6:00:00 and end at 18:30:00.

The first column corresponds to the actual date. Unfortunately, you can't use dates like this in R easily (the program we will use to graph the data), so make a second column (as in the example datasheet) with the days in order from 1 to the last day. IF YOU DON'T HAVE DATA FOR A GIVEN DAY, YOU STILL HAVE TO ENTER IT HERE. SEE BELOW!

Into the third column, paste the times from each worksheet. The only exception is that you need to add one entry before those times for the time you have decided to start your figure at. So, in the example worksheet, note that the first action in the worksheet from 1 Aug starts at 6:15:00, but in the final summary worksheet, the days' actions start at 6.

In the fourth column, use the equation =(C2-INT(C2))*24 to convert the time into a decimal time.

In the fifth column, describe the activity. Either "on", "off", or "?". Make sure you get the spelling and capitalization consistent.

In the sixth column, you have the time that that activity ended at, in decimals. So, simply have each cell in a day be filled with the cell one down from the row in question. The only trick to this is that for the last cell in a day, you need to hand type in the time you have chosen to end the figure at. In the example case, the figure will end at 18:15:00 or 18.25 in decimals.

Code the time between the start of the figure and the first activity as "?". The same applies for the time between the last activity and the end of the figure.

If you had a day for which no data was available, e.g. day 5 of the nestling period, you still need to enter a row for it in the final summary worksheet. The example row in the worksheet would read

day t activity tend
5 6:00:00 ? 18:00:00

After all this is done, take the 2nd, 4th, 5th and 6th columns and copy them. Paste special > values only into a new datasheet. Rename the columns "d", "t", "act", and "tend".

SAVE THIS FILE AS A CSV, not an xls or xlsx. Choose a simple file name, like "inc.csv" AN EXAMPLE CSV FILE IS INCLUDED HERE.

Download the free statistical program R.

Install the package ggplot2. You do this on a Mac by navigating within R to "Package Installer". On a PC it is something similar. MAKE SURE THAT YOU INSTALL IT WITH ALL DEPENDENCIES (this is a box you click when installing packages).

Set your working directory in R to be wherever your inc.csv (or whatever you called it) file is, e.g. the desktop.

Copy and paste the following lines into the R workspace BUT MAKE ONE IMPORTANT CHANGE FIRST! Where it says breaks=1:3 in the code below, replace the 3 with however many days you actually watched brooding or incubation for. In the example spreadsheet it is 3, but it could be many more for your species. After that, run the code and it will generate the plot and save it as a pdf to wherever your working directory is. You can convert this pdf into a jpg for your paper.

#####BROODING/INCUBATION CODE BEGINS HERE################

#set working directory to whatever is current, the first line is approximate

#load the package ggplot2

library(ggplot2)

#load your data file. change the name of the csv file if necessary

inc <- read.csv("inc.csv", stringsAsFactors = FALSE)

#this is the command to generate the plot

qplot(xmin=t, xmax=tend, ymin=d-0.45, ymax=d+0.45, data=inc, geom="rect", fill=act) + scale_y_reverse("Day", breaks = 1:3) + scale_x_continuous("Time") + scale_fill_manual(name = "Activity", values=c("gray80","white","black"), breaks = c("?","off","on"), labels = c("Not recorded", "Off nest", "On nest"))

#this command will save your image into the current working directory. you can change the shape of your image by dragging the corner. if you need to set a specific image dimension for a publication, there are ways to do this, e.g. in Mac with quartz(width= height=) etc., see R documentation for more details.

ggsave("brooding_figure.pdf")

#####BROODING/INCUBATION CODE ENDS HERE################

That should be all! If you like, cite this website to encourage others to use and improve this method--it should help to better visualize incubation rhythms and to facilitate comparisons between species, individuals and populations.

Thanks to Harold Greeney for first introducing me to the utility of such figures and to Hadley Wickham for help with the code.