library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(leaflet)
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(rgbif)
library(sp)
library(raster)
## 
## Attaching package: 'raster'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
library(dplyr)
library(leaflet.extras)

1:

# Getting the key for data
birdData <- occ_download_get(key = "0007596-240229165702484", overwrite = TRUE) %>%
  occ_download_import(bsbData, na.strings = c("", NA))
## Download file size: 13.66 MB
## On disk at C:\Users\Mohit Bhavsar\Documents\EEB4100\0007596-240229165702484.zip
birdData
## # A tibble: 81,040 × 223
##    gbifID accessRights bibliographicCitation language license modified publisher
##  * <int6> <lgl>        <lgl>                 <lgl>    <chr>   <lgl>    <lgl>    
##  1    4e9 NA           NA                    NA       CC_BY_… NA       NA       
##  2    4e9 NA           NA                    NA       CC_BY_… NA       NA       
##  3    4e9 NA           NA                    NA       CC_BY_… NA       NA       
##  4    4e9 NA           NA                    NA       CC_BY_… NA       NA       
##  5    4e9 NA           NA                    NA       CC_BY_… NA       NA       
##  6    4e9 NA           NA                    NA       CC_BY_… NA       NA       
##  7    4e9 NA           NA                    NA       CC_BY_… NA       NA       
##  8    4e9 NA           NA                    NA       CC_BY_… NA       NA       
##  9    4e9 NA           NA                    NA       CC_BY_… NA       NA       
## 10    4e9 NA           NA                    NA       CC_BY_… NA       NA       
## # ℹ 81,030 more rows
## # ℹ 216 more variables: references <lgl>, rightsHolder <lgl>, type <lgl>,
## #   institutionID <lgl>, collectionID <lgl>, datasetID <lgl>,
## #   institutionCode <chr>, collectionCode <chr>, datasetName <lgl>,
## #   ownerInstitutionCode <lgl>, basisOfRecord <chr>, informationWithheld <lgl>,
## #   dataGeneralizations <lgl>, dynamicProperties <lgl>, occurrenceID <chr>,
## #   catalogNumber <chr>, recordNumber <lgl>, recordedBy <chr>, …

2:

# Set specific datasets for plotting
winterOccurence <- filter(birdData, countryCode %in% c("US", "CA")) %>%
  slice(1:1000)
summerOccurence <- filter(birdData, countryCode %in% c("NI", "SV", "CR", "PA", "GT")) %>%
  slice(1:1000)

# Mapping out the datasets
bsbMap <- leaflet() %>% 
  addTiles() %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>% 
  addProviderTiles(providers$OpenTopoMap) %>%
 
  # Adds the markers for our species of birds
  addCircleMarkers(data = winterOccurence, 
                   ~decimalLongitude, ~decimalLatitude, 
                   color = "#9467bd", fillOpacity = 1, radius = 3, 
                   popup = ~paste(species, ":", eventDate),
                   group = "Winter Observations") %>%
  
  addCircleMarkers(data = summerOccurence, 
                   ~decimalLongitude, ~decimalLatitude, 
                   color = "black", fillOpacity = 2, radius = 3, 
                   popup = ~paste(species, ":", eventDate),
                   group = "Summer Observations") 

bsbMap

3:

# Create a new dataset that counts the number of times a city appears and then gets the top 3
observation_counts <- birdData %>%
  group_by(level2Name) %>%
  summarise(observation_count = n()) %>%
  arrange(desc(observation_count)) %>%
  slice(1:3)

# Maps out the coordinates associated with the specific cities
top_3_locations <- birdData %>%
  filter(level2Name %in% observation_counts$level2Name) %>%
  group_by(level2Name) %>%
  slice(1)

# Test to make sure it works
observation_counts
## # A tibble: 3 × 2
##   level2Name     observation_count
##   <chr>                      <int>
## 1 Division No. 6              4790
## 2 Cochise                     3678
## 3 Hidalgo                     2271
top_3_locations
## # A tibble: 3 × 223
## # Groups:   level2Name [3]
##    gbifID accessRights bibliographicCitation language license modified publisher
##   <int64> <lgl>        <lgl>                 <lgl>    <chr>   <lgl>    <lgl>    
## 1     4e9 NA           NA                    NA       CC_BY_… NA       NA       
## 2     4e9 NA           NA                    NA       CC_BY_… NA       NA       
## 3     4e9 NA           NA                    NA       CC_BY_… NA       NA       
## # ℹ 216 more variables: references <lgl>, rightsHolder <lgl>, type <lgl>,
## #   institutionID <lgl>, collectionID <lgl>, datasetID <lgl>,
## #   institutionCode <chr>, collectionCode <chr>, datasetName <lgl>,
## #   ownerInstitutionCode <lgl>, basisOfRecord <chr>, informationWithheld <lgl>,
## #   dataGeneralizations <lgl>, dynamicProperties <lgl>, occurrenceID <chr>,
## #   catalogNumber <chr>, recordNumber <lgl>, recordedBy <chr>,
## #   recordedByID <lgl>, individualCount <int>, organismQuantity <lgl>, …
# Puts the cities on the map using icons
bsbMap2 <- bsbMap %>%
  addAwesomeMarkers(data = top_3_locations,
             lng = ~decimalLongitude, lat = ~decimalLatitude,
             label = ~level2Name) 

bsbMap2

4:

# Finishing touches to the map
bsbMap3 <- bsbMap2 %>%
  
  # Toggle Birds
  addLayersControl(
    overlayGroups = c("Winter Observations", "Summer Observations"),
    options = layersControlOptions(collapsed = FALSE)) %>%
  
  # Adds Legends for the species and elevation
  addLegend(position = "bottomright", 
            colors = c("#9467bd", "black"), 
            labels = c("Buteo swainsoni Winter Observations", "Buteo swainsoni Summer Observations")) %>%
  addLegend("bottomleft", title = "Elevation",
            pal = colorNumeric(palette = c("lightgreen", "green", "darkgreen", "yellow", "orange", "red"), domain = c(0, 2000)),
            values = c(0, 2000),
            labels = c("0-200m", "201-400m", "401-600m", "601-800m", "801-1000m", "1001-2000m"))

bsbMap3

Citation: GBIF.org (02 March 2024) GBIF Occurrence Download https://doi.org/10.15468/dl.tqdh6q