Created
February 18, 2019 15:37
-
-
Save damianooldoni/17a6a29fdc6ef40d4f97e8f618a71bb6 to your computer and use it in GitHub Desktop.
Get number of GBIF occurrences per kingdom per year in Belgium with geographic coordinates.
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
library(tidyverse) | |
library(lubridate) | |
library(rgbif) | |
start_year <- 2000 | |
years <- seq(start_year, lubridate::year(Sys.Date())) | |
facet <- "kingdomKey" | |
# get counts per year for each kingdomKey | |
n_occ_kingdom <- map_dfr( | |
years, | |
function(t) { | |
rgbif::occ_search( | |
country = "BE", | |
year = t, | |
hasCoordinate = TRUE, | |
return = "facets", | |
facet = facet, | |
limit = 0)$facets[[facet]] %>% | |
rename(kingdomKey = name) %>% | |
mutate(year = t) | |
} | |
) | |
# retrrieve kingdom names | |
kingdom_names <- | |
n_occ_kingdom %>% | |
distinct(kingdomKey) %>% | |
group_by(kingdomKey) %>% | |
mutate( | |
kingdom = rgbif::name_usage(key = kingdomKey, return = "data")$kingdom) | |
# add kingdom names to counts df | |
n_occ_kingdom <- | |
n_occ_kingdom %>% | |
left_join( | |
kingdom_names, | |
by = "kingdomKey") %>% | |
select(kingdomKey, kingdom, dplyr::everything()) | |
# save plot in root | |
map(unique(kingdom_names$kingdom), | |
function(k) { | |
ggplot(n_occ_kingdom %>% | |
filter(kingdom == k), | |
aes(x = year, y = count)) + | |
geom_line() + | |
geom_point(size=4, fill = "red") + | |
ggtitle(k) + | |
ggsave(paste0("n_occ_",k,".png"), device = "png") | |
} | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment