Skip to content

Instantly share code, notes, and snippets.

@J-Cleeland
Last active June 18, 2018 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save J-Cleeland/7049908947dae9067ad9bfdb914ac032 to your computer and use it in GitHub Desktop.
Save J-Cleeland/7049908947dae9067ad9bfdb914ac032 to your computer and use it in GitHub Desktop.
Circular timeline of Southern Ocean albatross breeding cycles using ggplot. Note: BBA, black-browed albatross; LMA, light-mantled albatross; GHA, grey-headed albatross; WA, Wandering albatross.
---
title: "albatross_breeding_cycle"
author: "Jaimie Cleeland jaimie.cleeland@utas.edu.au"
date: "12/12/2016"
output: html_document
---
Load libraries
```{r}
library(dplyr)
library(ggplot2)
library(lubridate)
library(RColorBrewer)
library(grDevices)
library(gridExtra)
library(RColorBrewer)
library(grid)
library(gtable)
```
Make data frame of albatross breeding dates
```{r}
bba_bp <- seq(as.Date("2001-09-25"), as.Date("2002-09-24"), "days")
bba_n <- rep("black-browed albatross", length(bba_bp))
gha_bp <- seq(as.Date("2003-10-06"), as.Date("2005-10-05"), "days")
gha_n <- rep("grey-headed albatross", length(gha_bp))
lma_bp <- seq(as.Date("2006-10-17"), as.Date("2008-10-16"), "days")
lma_n <- rep("light-mantled albatross", length(lma_bp))
wa_bp <- seq(as.Date("2009-12-08"), as.Date("2012-12-07"), "days")
wa_n <- rep("Wandering albatross", length(wa_bp))
dat <- data.frame(species= c(bba_n, gha_n, lma_n, wa_n), date= c(bba_bp, gha_bp, lma_bp, wa_bp), stringsAsFactors = F)
dat$day <- as.numeric(strftime(dat$date, format = "%j"))
dat$stage <- NA
dat$stage[dat$species=="black-browed albatross"&dat$date%in%seq(as.Date("2001-09-25"), as.Date("2002-01-08"), "days")] <- "incubation"
dat$stage[dat$species=="black-browed albatross"&dat$date%in%seq(as.Date("2002-01-09"), as.Date("2002-05-10"), "days")] <- "chick-rearing"
dat$stage[dat$species=="black-browed albatross"&dat$date%in%seq(as.Date("2002-05-11"), as.Date("2002-09-24"), "days")] <- "non-breeding"
dat$stage[dat$species=="grey-headed albatross"&dat$date%in%seq(as.Date("2003-10-06"), as.Date("2004-01-09"), "days")] <- "incubation"
dat$stage[dat$species=="grey-headed albatross"&dat$date%in%seq(as.Date("2004-01-10"), as.Date("2004-05-25"), "days")] <- "chick-rearing"
dat$stage[dat$species=="grey-headed albatross"&dat$date%in%seq(as.Date("2004-05-26"), as.Date("2005-10-05"), "days")] <- "non-breeding"
dat$stage[dat$species=="light-mantled albatross"&dat$date%in%seq(as.Date("2006-10-17"), as.Date("2006-12-31"), "days")] <- "incubation"
dat$stage[dat$species=="light-mantled albatross"&dat$date%in%seq(as.Date("2007-01-01"), as.Date("2007-06-15"), "days")] <- "chick-rearing"
dat$stage[dat$species=="light-mantled albatross"&dat$date%in%seq(as.Date("2007-06-16"), as.Date("2008-10-16"), "days")] <- "non-breeding"
dat$stage[dat$species=="Wandering albatross"&dat$date%in%seq(as.Date("2009-12-08"), as.Date("2010-03-05"), "days")] <- "incubation"
dat$stage[dat$species=="Wandering albatross"&dat$date%in%seq(as.Date("2010-03-06"), as.Date("2011-01-01"), "days")] <- "chick-rearing"
dat$stage[dat$species=="Wandering albatross"&dat$date%in%seq(as.Date("2011-01-02"), as.Date("2011-12-07"), "days")] <- "non-breeding"
```
```{r}
dat$group <- paste0(dat$species, "_", dat$stage)
```
Add month label reference objects.
```{r}
months <- as.numeric(strftime(seq(as.POSIXct("2004-01-01"), as.POSIXct("2004-12-31"), "month"), format = "%j"))
month_lab <- strftime(seq(as.POSIXct("2004-01-01"), as.POSIXct("2004-12-31"), "month"), format = "%b")
```
```{r}
dat$date <- as.POSIXct(dat$date)
```
```{r}
#BBA, WA, LMA, GHA
cols <- brewer.pal(5, "Set1")[2:5]
bba_cols <- c(adjustcolor(cols[4], alpha.f = 0.9), adjustcolor(cols[4], alpha.f = 0.6), adjustcolor(cols[4], alpha.f = 0.3))
gha_cols <- c(adjustcolor(cols[3], alpha.f = 0.9), adjustcolor(cols[3], alpha.f = 0.6), adjustcolor(cols[3], alpha.f = 0.3))
lma_cols <- c(adjustcolor(cols[2], alpha.f = 0.9), adjustcolor(cols[2], alpha.f = 0.6), adjustcolor(cols[2], alpha.f = 0.3))
wa_cols <- c(adjustcolor(cols[1], alpha.f = 0.9), adjustcolor(cols[1], alpha.f = 0.6), adjustcolor(cols[1], alpha.f = 0.3))
cols <- c(bba_cols, gha_cols, lma_cols, wa_cols)
cols2 <- data.frame(group = unique(dat$group), colours=cols, stringsAsFactors = F)
```
Create plot
```{r}
b <- ggplot(dat, aes(x=as.numeric(day), xend=as.numeric(day)+1, y=date, yend=date, colour=group)) +
geom_segment(data=dat[dat$group==cols2$group[1],], colour=cols2$colours[1], size=2) +
geom_segment(data=dat[dat$group==cols2$group[2],], colour=cols2$colours[2], size=2) +
geom_segment(data=dat[dat$group==cols2$group[3],], colour=cols2$colours[3], size=2) +
geom_segment(data=dat[dat$group==cols2$group[4],], colour=cols2$colours[4], size=2) +
geom_segment(data=dat[dat$group==cols2$group[5],], colour=cols2$colours[5], size=2) +
geom_segment(data=dat[dat$group==cols2$group[6],], colour=cols2$colours[6], size=2) +
geom_segment(data=dat[dat$group==cols2$group[7],], colour=cols2$colours[7], size=2) +
geom_segment(data=dat[dat$group==cols2$group[8],], colour=cols2$colours[8], size=2) +
geom_segment(data=dat[dat$group==cols2$group[9],], colour=cols2$colours[9], size=2) +
geom_segment(data=dat[dat$group==cols2$group[10],], colour=cols2$colours[10], size=2) +
geom_segment(data=dat[dat$group==cols2$group[11],], colour=cols2$colours[11], size=2) +
geom_segment(data=dat[dat$group==cols2$group[12],], colour=cols2$colours[12], size=2) +
scale_x_continuous(limits=c(1,366), breaks=months, minor_breaks=months, labels = month_lab) +
scale_y_datetime(limits=c(min(dat$date)-60*60*24*365, max(dat$date)), breaks=seq(min(dat$date), max(dat$date),"1 year"), date_labels="%y") +
coord_polar() +
labs(x="",y="") +
theme(panel.grid.major.x = element_line(colour = "grey89", size=0.3),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.background = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(family = "Times", colour = "black", size = 5),
legend.position="none",
plot.margin=unit(c(0,0.6,-0.5,-0.6),"cm"))
```
```{r}
b
```
```{r}
ggsave("Albatross_breeding_cycle_circle.tiff", plot = b, device = NULL, path = "/Users/MyMac/Documents/", scale = 1, width = 6.6, height = 6, units = c("cm"), dpi = 300)
```
```{r}
al <- data.frame(incubation=c("", "", "", ""), chick_rearing=c("", "", "", ""), nonbreeding=c("", "", "", ""))
rownames(al) <- c("BBA", "GHA", "LMA", "WA")
cn1 <- expression(paste("incubation"))
cn2 <- expression(paste("chick", "-", "rearing"))
cn3 <- expression(paste("nonbreeding"))
colnames(al) <- c(cn1, cn2, cn3)
```
```{r}
dev.off()
tt <- ttheme_minimal(
core=list(fg_params=list(fontface=1, fontfamily="Times", cex=0.55, rot=0), padding=unit(c(2, 2), "mm")),
colhead=list(fg_params=list(col="grey39", fontface=1, fontfamily="Times", cex=0.8, rot=90, parse=T, hjust=0, y=0.1), padding=unit(c(2, 2), "mm")),
rowhead=list(fg_params=list(col="grey39", fontface=1, fontfamily="Times", cex=0.8, rot=0, parse=T), padding=unit(c(2, 2), "mm")))
g <- tableGrob(al, theme=tt)
grid.draw(g)
```
```{r}
g$grobs[29][[1]][["gp"]] <- gpar(fill=cols[1], col = "white", lwd=2) #WA
g$grobs[30][[1]][["gp"]] <- gpar(fill=cols[4], col = "white", lwd=2) #LMA
g$grobs[31][[1]][["gp"]] <- gpar(fill=cols[7], col = "white", lwd=2) #GHA
g$grobs[32][[1]][["gp"]] <- gpar(fill=cols[10], col = "white", lwd=2) #BBA
g$grobs[33][[1]][["gp"]] <- gpar(fill=cols[2], col = "white", lwd=2) #WA
g$grobs[34][[1]][["gp"]] <- gpar(fill=cols[5], col = "white", lwd=2) #LMA
g$grobs[35][[1]][["gp"]] <- gpar(fill=cols[8], col = "white", lwd=2) #GHA
g$grobs[36][[1]][["gp"]] <- gpar(fill=cols[11], col = "white", lwd=2) #BBA
g$grobs[37][[1]][["gp"]] <- gpar(fill=cols[3], col = "white", lwd=2) #WA
g$grobs[38][[1]][["gp"]] <- gpar(fill=cols[5], col = "white", lwd=2) #LMA
g$grobs[39][[1]][["gp"]] <- gpar(fill=cols[9], col = "white", lwd=2) #GHA
g$grobs[40][[1]][["gp"]] <- gpar(fill=cols[12], col = "white", lwd=2) #BBA
```
```{r}
dev.off()
grid.draw(g)
```
```{r}
tiff(file="/Users/jaimiec/Documents/PhD/Draft publications/Tracking_Paper/Data summaries/Legend.tiff",width=2.5,height=6.8,units="cm",res=500)
par(mar=c(0,0,1,0), family="serif",bg=NA)
plot(1, type="n", axes=F, xlab="", ylab="")
mtext(text = "Albatross\nbreeding cycle", side = 3, line = -2, cex=0.8)
grid.draw(g)
dev.off()
```
@J-Cleeland
Copy link
Author

figure1_breeding_cycle_circle copy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment