Loading libraries

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)
## Warning: package 'rgbif' was built under R version 4.3.3
library(sp)
library(raster)
## 
## Attaching package: 'raster'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
library(dplyr)
library(leaflet.extras)
## Warning: package 'leaflet.extras' was built under R version 4.3.3
# Get the data from GBIF
narwData <- occ_download_get(key = "0157193-240321170329656", overwrite = TRUE) %>%
  occ_download_import(bsbData, na.strings = c("", NA))
## Download file size: 0.24 MB
## On disk at C:\Users\arori\Documents\EEB 4100\0157193-240321170329656.zip
narwData
## # A tibble: 6,777 × 50
##        gbifID datasetKey    occurrenceID kingdom phylum class order family genus
##  *    <int64> <chr>         <chr>        <chr>   <chr>  <chr> <chr> <chr>  <chr>
##  1  785358854 4bfac3ea-876… MCZ:Mamm:67… Animal… Chord… Mamm… Ceta… Balae… Euba…
##  2  476857057 4bfac3ea-876… MCZ:Mamm:64… Animal… Chord… Mamm… Ceta… Balae… Euba…
##  3  476847471 4bfac3ea-876… MCZ:Mamm:64… Animal… Chord… Mamm… Ceta… Balae… Euba…
##  4  476813429 4bfac3ea-876… MCZ:Mamm:66… Animal… Chord… Mamm… Ceta… Balae… Euba…
##  5 4607576323 50c9509d-22c… https://www… Animal… Chord… Mamm… Ceta… Balae… Euba…
##  6 4607491009 50c9509d-22c… https://www… Animal… Chord… Mamm… Ceta… Balae… Euba…
##  7 4606697274 50c9509d-22c… https://www… Animal… Chord… Mamm… Ceta… Balae… Euba…
##  8 4600032213 50c9509d-22c… https://www… Animal… Chord… Mamm… Ceta… Balae… Euba…
##  9 4528040951 50c9509d-22c… https://www… Animal… Chord… Mamm… Ceta… Balae… Euba…
## 10 4525100192 50c9509d-22c… https://www… Animal… Chord… Mamm… Ceta… Balae… Euba…
## # ℹ 6,767 more rows
## # ℹ 41 more variables: species <chr>, infraspecificEpithet <lgl>,
## #   taxonRank <chr>, scientificName <chr>, verbatimScientificName <chr>,
## #   verbatimScientificNameAuthorship <chr>, countryCode <chr>, locality <chr>,
## #   stateProvince <chr>, occurrenceStatus <chr>, individualCount <int>,
## #   publishingOrgKey <chr>, decimalLatitude <dbl>, decimalLongitude <dbl>,
## #   coordinateUncertaintyInMeters <dbl>, coordinatePrecision <dbl>, …
# Filter data from before 2010 (includes both winter and summer)
oldWinterSight <- filter(narwData, month %in% c("1", "2", "3", "10", "11", "12"), year <= 2010, decimalLongitude <= -45)
oldSummerSight <- filter(narwData, month %in% c("4", "5", "6", "7", "8", "9"), year <= 2010, decimalLongitude <= -45)

# Filter  data from after 2010 (includes both winter and summer)
newWinterSight <- filter(narwData, month %in% c("1", "2", "3", "10", "11", "12"), year > 2010, decimalLongitude <= -45)
newSummerSight <- filter(narwData, month %in% c("4", "5", "6", "7", "8", "9"), year > 2010, decimalLongitude <= -45)
# Creates a leaflet (used example from lab 5)
migrationMap <- leaflet() %>% 
  addTiles() %>%
  addProviderTiles(providers$Stadia.AlidadeSmooth) %>% # Set theme
 
  # Set markers for pre-2010 winter sightings
  addCircleMarkers(data = oldWinterSight, 
                   ~decimalLongitude, ~decimalLatitude, 
                   color = "gray", fillOpacity = 1, radius = 3, 
                   popup = ~paste(eventDate,":", decimalLatitude,",", decimalLongitude),
                   group = "Pre 2010 Winter Sightings") %>%
  
  # Set markers for pre-2010 summer sightings
  addCircleMarkers(data = oldSummerSight, 
                   ~decimalLongitude, ~decimalLatitude, 
                   color = "blue", fillOpacity = 2, radius = 3, 
                   popup = ~paste(eventDate,":", decimalLatitude,",", decimalLongitude),
                   group = "Pre 2010 Summer Sightings") %>%

  # Set markers for post-2010 winter sightings
  addCircleMarkers(data = newWinterSight, 
                   ~decimalLongitude, ~decimalLatitude, 
                   color = "red", fillOpacity = 1, radius = 3, 
                   popup = ~paste(eventDate,":", decimalLatitude,",", decimalLongitude),
                   group = "Post 2020 Winter sightings") %>%
  
  # Set markers for post-2010 summer sightings
  addCircleMarkers(data = newSummerSight, 
                   ~decimalLongitude, ~decimalLatitude, 
                   color = "orange", fillOpacity = 1, radius = 3, 
                   popup = ~paste(eventDate,":", decimalLatitude,",", decimalLongitude),
                   group = "Post 2020 Summer Sightings") 

migrationMap
# Made to enable layers
newNARWMap <- migrationMap %>%
  addLayersControl(
    overlayGroups = c("Pre 2010 Winter Sightings", "Pre 2010 Summer Sightings", "Post 2020 Winter sightings", "Post 2020 Summer Sightings"),
    options = layersControlOptions(collapsed = FALSE)) %>%
  addLegend(position = "bottomright", 
            colors = c("gray", "blue", "red", "orange"), 
            labels = c("Pre 2010 Winter Sightings", "Pre 2010 Summer Sightings", "Post 2010 Winter Sightings", "Post 2010 Winter Sightings"))

newNARWMap