Created
April 19, 2020 08:54
-
-
Save davidclarance/14fc6fde0c75e87cb916c162554b176b to your computer and use it in GitHub Desktop.
Create animated maps to show changes in reporting rates across the year in Kenya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 0. set up --------------------------------------------------------------- | |
# libraries | |
library(rabm) | |
library(tidyverse) | |
library(geojsonio) | |
library(broom) | |
library(ggplot2) | |
library(gganimate) | |
library(transformr) | |
# atlas data | |
# needs to be split up because of API restrictions | |
kenya_pentads_split_1 <- rabm::extract_all( | |
start_date = '2007-01-01', | |
end_date = '2018-04-15', | |
region_type = "country", | |
region_id = "kenya") | |
kenya_pentads_split_2 <- rabm::extract_all( | |
start_date = '2018-04-15', | |
end_date = '2020-04-15', | |
region_type = "country", | |
region_id = "kenya") | |
kenya_pentad_data <- dplyr::bind_rows( | |
kenya_pentads_split_1, | |
kenya_pentads_split_2 | |
) | |
# species list | |
species_list <- rabm::get_species_list("kenya") | |
# spatial data | |
# download from: http://api.adu.org.za/sabap2/v2/coverage/project/kenya?format=geoJSON | |
# save it to your machine and replace the location below | |
spdf <- geojsonio::geojson_read("../Dropbox/BirdData/kenya_grid.geojson", what = "sp") | |
spdf_fortified <- broom::tidy(spdf) | |
spdf_fortified$Pentad <- rep(spdf@data[["pentad"]], each = 5) # rep 5 times because there are 5 coordinates per pentad.s | |
# 1. function to make animation ---------------------------------------------- | |
make_species_animation <- function(species_id, | |
species_name, | |
country_data, | |
fortified_spatial_df){ | |
# get reporting rate per pentad-week | |
rr_pentad_week <- country_data %>% | |
mutate(WeekOfYear = lubridate::week(StartDate)) %>% | |
group_by(Pentad, WeekOfYear) %>% | |
summarize( | |
NumberOfLists = n_distinct(CardNo), | |
CountOfSpecies = sum(if_else(Spp == species_id, 1, 0)), | |
ReportingRate = (CountOfSpecies / NumberOfLists) | |
) %>% | |
arrange(Pentad, WeekOfYear) | |
# make df to plot | |
spdf_plot <- fortified_spatial_df %>% | |
# first make a base dataset with all weeks | |
select( | |
long, | |
lat, | |
group, | |
id, | |
Pentad | |
) %>% | |
slice(rep(1:n(), | |
53)) %>% | |
mutate(WeekOfYear = rep(1:53, each = n()/53)) %>% | |
# add in the reporting rates | |
left_join(rr_pentad_week, by = c("Pentad", "WeekOfYear")) %>% | |
# cards that don't have a reporting rate are set to 0 | |
# this is weird but helps with the graph... | |
# need to work on this a bit later | |
replace_na(list(ReportingRate = 0)) %>% | |
select( | |
long, | |
lat, | |
group, | |
id, | |
Pentad, | |
WeekOfYear, | |
ReportingRate | |
) | |
# base plot | |
base_plot <- ggplot(data = spdf_plot, | |
aes( x = long, | |
y = lat, | |
group = group, | |
fill= ReportingRate)) + | |
theme_bw() + | |
scale_fill_gradient(low = "#E8E8E8", high = "red") + | |
geom_polygon(color="#E8E8E8") + | |
coord_map() + | |
labs(title = glue::glue("Reporting Rates in Kenya: {species_name}"), | |
subtitle = 'Week Number: {round(frame_time, 0)}') + | |
transition_time(WeekOfYear) + | |
ease_aes('linear') | |
# animate base plot | |
final_animation <- animate(base_plot, | |
duration = 30) | |
return(final_animation) | |
} | |
# 2. build animation --------------------------------------------------------- | |
# some examples | |
red_capped_robin_chat <- make_species_animation( | |
species_id = 579, | |
species_name = "Red-capped Robin Chat", | |
country_data = kenya_pentad_data, | |
fortified_spatial_df = spdf_fortified) | |
hadada_ibis <- make_species_animation( | |
species_id = 84, | |
species_name = "Hadada Ibis", | |
country_data = kenya_pentad_data, | |
fortified_spatial_df = spdf_fortified) | |
spotted_flycatcher <- make_species_animation( | |
species_id = 654, | |
species_name = "Spotted Flycatcher", | |
country_data = kenya_pentad_data, | |
fortified_spatial_df = spdf_fortified) | |
barn_swallow <- make_species_animation( | |
species_id = 493, | |
species_name = "Barn Swallow", | |
country_data = kenya_pentad_data, | |
fortified_spatial_df = spdf_fortified) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment