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!

 

 

Advertisements

[D-3] Belgica120!

3 more days for the crew to begin their journey to Antarctica Peninsula through Argentina!! Excited about this low-emission expedition? Remember to bookmark and checkout their blog!!

We wish the crew all the best for the expedition! May the weather be kind to you and have a fruitful journey! \o/

Concours Antarctique

Nous organisons un concours d’histoires sur l’Antarctique destiné aux élèves de la 5e primaire à la 1ère secondaire!

Le principe est simple : choisissez l’une des 22 images proposées et écrivez une histoire courte de 2 à 3 pages sur le thème « l’Antarctique ».
10 histoires francophones et 10 histoires néerlandophones seront rassemblées dans en livre et imprimées. Chaque école participante et les élèves dont l’histoire aura été sélectionnée recevront un exemplaire. Les gagnants recevront également la visite d’un scientifique polaire dans leur classe.

Bon amusement!!

230_230_Climat-3

R: Visualize occurrence records by binning

Let’s get a dataset from OBIS using robis, R client for OBIS API.

library(robis)
occ <- occurrence(resourceid = 2296)

The function occurrence() get occurrence records from OBIS which has resourceid = 2296 and transform the results into an R dataframe. To quickly visualize the occurrences, we can do

library(obistools)
plot_map(occ)

plot_map

There are a lot of points overlapping each other.  We can bin these points into hexagons/squares and colour these hexagons/squares based on the number of points in each hexagon. To achieve this, we can use the ly_hexbin function from rbokeh.

# hexbin with rbokeh
library(rbokeh)
library(dplyr)
attach(occ)
figure(width = 900, height = 450) %>%
 ly_map("world", color = "#707070", alpha = 0.8) %>% 
 ly_hexbin(decimalLongitude, decimalLatitude, xbins = 100, shape = 0.2, 
 alpha = 0.8, palette = "Spectral6", trans = log, inv = exp) %>% 
 theme_plot(background_fill_color = "black") %>% # dark background
 theme_grid(grid_line_alpha = 0) # remove grid

rbokeh_log

Warmer colour means more occurrences within that hexagon. The colour is based on log scale of the count.

It is nicer, but we could have done better! A lot of points are scatter around antarctic/subantarctic zone. It could be nicer to make the plot in polar projection. In this case, we can do that with ggplot!

library(ggplot2)
library(viridis)
world <- map_data("world")
ggplot() + 
 geom_bin2d(data = occ, aes(x = decimalLongitude, y = decimalLatitude), bins = 100) +
 geom_path(data = world, aes(x = long, y = lat, group = group), colour = "#c0c0c0") +
 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)) + # orthographic projection from South Pole
 scale_fill_viridis(option = "viridis", trans = "log") + # log scale for bin count
 theme(panel.background = element_rect("black"), # dark background
 panel.grid = element_blank(), # remove panel grid
 axis.text.x = element_blank()) # remove x-axis value

ggplot-polar

Tada!! 🙂 We will post more interesting tutorials later! Thanks for reading!!

Credit:

British Antarctic Survey. SOMBASE PYCNOGONIDS. Occurrence Dataset https://doi.org/10.15468/qtm508 accessed via iobis.org on 2018-02-11.

 

Data, data, data, …

We live in the area of big data. As scientists, but also communicators it is important to be able to handle and understand large (and small…) amounts of data. APECS Belgium therefore introduces a new category on our website: data tutorials.

Yi Ming Gan has written before about how data science can aid polar research. Now she’s contributed our first data tutorial. Check it out!