Skip to content

Instantly share code, notes, and snippets.

View robwschlegel's full-sized avatar

Robert William Schlegel robwschlegel

View GitHub Profile
baja_pen_map <- plot_sites(baja_pen, 1, 1)
baja_pen_seq_map <- plot_sites(baja_pen_seq, 1, 2)
baja_pen_island_seq_map <- plot_sites(baja_pen_island_seq, 1, 3)
grid.arrange(baja_pen_map, baja_pen_seq_map, baja_pen_island_seq_map, nrow = 1)
south_africa_map <- plot_sites(south_africa, 1, 1)
south_africa_seq_map <- plot_sites(south_africa_seq, 1, 2)
grid.arrange(south_africa_map, south_africa_seq_map, nrow = 1)
cape_point_map <- plot_sites(cape_point, 0.5, 1)
cape_point_seq_map <- plot_sites(cape_point_seq, 0.5, 2)
grid.arrange(cape_point_map, cape_point_seq_map, nrow = 1)
# Create base map
world_map <- ggplot() +
borders(fill = "grey40", colour = "black")
# Create titles
titles <- c("Deurmekaar", "Sequential", "Islands")
# Plotting function
plot_sites <- function(site_list, buffer, title_choice){
world_map +
# NB: This code will produce warnings
# This is fine as it is stating that the
# 'order' column has been re-written,
# which is the intended result of this function.
# Cape Point, South Africa
cape_point_seq <- seq_sites(cape_point)
# South Africa
south_africa_seq <- seq_sites(south_africa)
# Cape Point, South Africa
cape_point <- SACTN_site_list %>%
slice(c(31, 22, 26, 17, 19, 21, 30)) %>%
mutate(order = 1:n())
# South Africa
south_africa <- SACTN_site_list %>%
slice(c(1,34, 10, 20, 50, 130, 90)) %>%
mutate(order = 1:n())
# By default 'saveGIF()' outputs to the same folder
# the script where the code is being run from is located.
# For that reason one may want to manually change the
# working directory beforehand.
# setwd("somewhere else")
system.time(saveGIF(animate.polar.plot(df = PN), interval = 0.4, ani.width = 457,
movie.name = "polar_plot_PN.gif")) ## 262 seconds
system.time(saveGIF(animate.polar.plot(df = SP), interval = 0.4, ani.width = 457,
movie.name = "polar_plot_SP.gif")) ## 221 seconds
system.time(saveGIF(animate.polar.plot(df = KB), interval = 0.4, ani.width = 457,
## Function that creates a polar plot
polar.plot <- function(df, i){
# Add bridges for polar coordinates
years <- unique(df$year)[1:i]
df2 <- filter(df, year %in% years)
bridges <- df2[df2$month == 'Jan',]
bridges$year <- bridges$year - 1
if(nrow(bridges) == 0){
bridges <- data.frame(site = df2$site[1], year = min(df2$year), month = NA, anom = NA)
} else {
## Libraries
library(tidyverse)
library(viridis)
library(lubridate)
library(gridExtra)
library(animation)
## Data
# SACTN
load("~/SACTN/data/SACTNmonthly_v4.1.Rdata")
# Convert the figures to grobs
mg_top_grob <- ggplotGrob(mg_top)
fb_grob <- ggplotGrob(fb)
mg_bottom_grob <- ggplotGrob(mg_bottom)
# Stick them together
gg <- ggplot() +
# First set the x and y axis values so we know what the ranges are
# in order to make it easier to place our facets
coord_equal(xlim = c(1, 10), ylim = c(1, 10), expand = F) +