[R]Animate time-series occurrence data

Objective

To animate occurrences of threatened antarctic/subantarctic species in year 2003 by month.

Load libraries

ImageMagick is required in order to use gganimate convert().

library(jsonlite)
library(maps)
library(mapproj)
library(robis)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(gganimate)  # require ImageMagick

Request using IUCN redlist API

Note: You will need a token from IUCN redlist to do this.

Request for all IUCN redlist species with country code = AQ (antarctica)

request <- paste("http://apiv3.iucnredlist.org/api/v3/country/getspecies/AQ?token=", token, sep = "")
rl <- fromJSON(request)
rl <- rl$result

Subset data frame to entries with vulnerable, critical and endangered species only.

# filter for results with categories == vulnerable, critical and endangered (threatened)
not.lc.dd <- rl %>% filter(category %in% c("CR", "EN", "VU"))
unique(not.lc.dd$category)
## [1] "VU" "CR" "EN"

Request for occurrences using robis

Request for all occurrence records of these threatened species within antarctic and subantarctic zone from OBIS.

# get the occurrence of these threatened species from OBIS using API
# polygon include antarctic and subantarctic area
occ <- occurrence(scientificname = not.lc.dd$scientific_name, geometry = "POLYGON((180 -90, -180 -90, -180 -45, 180 -45, 180 -90))")

# convert eventDate from string to date time format
occ$eventDate <- as.POSIXct(strptime(occ$eventDate, format = "%Y-%m-%d %H:%M:%S", tz = "GMT"))
occ$month <- as.numeric(format(occ$eventDate, "%m"))
occ$year <- as.numeric(format(occ$eventDate, "%Y"))
obs <- occ %>% filter(year == 2003)

# remove preservedSpecimen occurrences
obs <- occ %>% filter(basisOfRecord != "PreservedSpecimen")

Plot orthographic projected map and animate with gganimate

Plot map with orthographic projection from South Pole and add a layer of occurrences, color by genus.

Animate the plots and create a gif file called occ_by_month.gif

# plot map with orthographic projection from South Pole
world <- map_data("world")
head(world)
##        long      lat group order region subregion
## 1 -69.89912 12.45200     1     1  Aruba      
## 2 -69.89571 12.42300     1     2  Aruba      
## 3 -69.94219 12.43853     1     3  Aruba      
## 4 -70.00415 12.50049     1     4  Aruba      
## 5 -70.06612 12.54697     1     5  Aruba      
## 6 -70.05088 12.59707     1     6  Aruba      
p <- ggplot() + 
  geom_path(data = world, aes(x = long, y = lat, group = group), colour = "#707070") +
  scale_y_continuous(name = "latitude", breaks = (-2:2) * 30) + 
  scale_x_continuous(name = "longitude", breaks = (-4:4) * 45) + 
  coord_map("ortho", orientation = c(-90, 0, 0)) +
  geom_point(data = obs, aes(x = decimalLongitude, y = decimalLatitude, color = genus, frame = month), size = 1, alpha = 0.5) +
  scale_color_brewer(palette = "Dark2")

# animate it
animation <- gganimate(p, interval=.5, "occ_by_month.gif")
animation
occ_by_month
Animate occurrence data for year 2003 using gif. Top left corner shows month.

Remarks

Please note that this exercise is not based on science. For instance, there are a lot more occurrence records from November to March, maybe because there are more people at field in Antarctica during that period. I am assuming that all occurrences here means presence. Not observing an occurrence do not means its absence either.

That’s it for now!

 

 

Tagged