step 0: setup and load required packages

setwd('/Users/bellaharding/Desktop/EEB4100_R')
# need to install the package before it can be loaded
#install.packages("rgbif")
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.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(rgbif)
library(dplyr)
library(RColorBrewer)
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## 
## The following object is masked from 'package:dplyr':
## 
##     select

step 1: load in the data set. I chose to work with data about the species Selasphorus rufus, aka the rufous hummingbird. This bird is known for its extremely long migrations, flying up to 2000 miles! Citation for the data set: GBIF.org (29 February 2024) GBIF Occurrence Download https://doi.org/10.15468/dl.762vb2 URL for the data set: https://www.gbif.org/occurrence/download/0025433-240216155721649

# load the data set into the workbook
seru_gbif <- occ_download_get( key = "0007758-240229165702484", overwrite = TRUE) %>%
  occ_download_import(seru_gbif, na.strings = c("", NA))
## Download file size: 0.59 MB
## On disk at /Users/bellaharding/Desktop/EEB4100_R/0007758-240229165702484.zip
# quick view of the data set
head(seru_gbif)
## # A tibble: 6 × 50
##   gbifID datasetKey occurrenceID kingdom phylum class order family genus species
##   <int6> <chr>      <chr>        <chr>   <chr>  <chr> <chr> <chr>  <chr> <chr>  
## 1    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 2    9e8 50c9509d-… http://cona… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 3    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 4    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 5    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 6    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## # ℹ 40 more variables: infraspecificEpithet <lgl>, taxonRank <chr>,
## #   scientificName <chr>, verbatimScientificName <chr>,
## #   verbatimScientificNameAuthorship <lgl>, countryCode <chr>, locality <lgl>,
## #   stateProvince <chr>, occurrenceStatus <chr>, individualCount <lgl>,
## #   publishingOrgKey <chr>, decimalLatitude <dbl>, decimalLongitude <dbl>,
## #   coordinateUncertaintyInMeters <dbl>, coordinatePrecision <lgl>,
## #   elevation <lgl>, elevationAccuracy <lgl>, depth <lgl>, …
#rename the columns storing latitude and longitude data
seru_gbif <- dplyr::rename(seru_gbif, latitude = decimalLatitude, longitude = decimalLongitude)
head(seru_gbif)
## # A tibble: 6 × 50
##   gbifID datasetKey occurrenceID kingdom phylum class order family genus species
##   <int6> <chr>      <chr>        <chr>   <chr>  <chr> <chr> <chr>  <chr> <chr>  
## 1    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 2    9e8 50c9509d-… http://cona… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 3    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 4    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 5    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## 6    9e8 50c9509d-… http://www.… Animal… Chord… Aves  Apod… Troch… Sela… Selasp…
## # ℹ 40 more variables: infraspecificEpithet <lgl>, taxonRank <chr>,
## #   scientificName <chr>, verbatimScientificName <chr>,
## #   verbatimScientificNameAuthorship <lgl>, countryCode <chr>, locality <lgl>,
## #   stateProvince <chr>, occurrenceStatus <chr>, individualCount <lgl>,
## #   publishingOrgKey <chr>, latitude <dbl>, longitude <dbl>,
## #   coordinateUncertaintyInMeters <dbl>, coordinatePrecision <lgl>,
## #   elevation <lgl>, elevationAccuracy <lgl>, depth <lgl>, …

I will select a few columns of the data set to parse it down a bit, as well as drop incidences without coordinates This includes species info, location info, and event data. Another column is also added with the season

seru_select <- seru_gbif %>% dplyr::select(species, countryCode, locality, stateProvince, latitude, 
                                    longitude, elevation, eventDate, month) %>%
  drop_na(latitude) %>% drop_na(longitude) %>%
 # i know this step is kinda unnecessary haha 
   mutate(Season = case_when(
    month <= 2 | month == 12 ~ "Winter",
    month >= 3 & month <= 5 ~ "Spring",
    month >= 6 & month <= 8 ~ "Summer",
    month >= 9 & month <= 11 ~ "Autumn"
  ))
head(seru_select)
## # A tibble: 6 × 10
##   species        countryCode locality stateProvince latitude longitude elevation
##   <chr>          <chr>       <lgl>    <chr>            <dbl>     <dbl> <lgl>    
## 1 Selasphorus r… US          NA       California        39.4    -120.  NA       
## 2 Selasphorus r… MX          NA       Michoacán         19.6    -101.  NA       
## 3 Selasphorus r… US          NA       New Mexico        35.1    -107.  NA       
## 4 Selasphorus r… US          NA       Georgia           34.1     -84.6 NA       
## 5 Selasphorus r… US          NA       Georgia           34.1     -84.6 NA       
## 6 Selasphorus r… US          NA       Georgia           34.1     -84.6 NA       
## # ℹ 3 more variables: eventDate <chr>, month <int>, Season <chr>
# Now I am going to select data that is only from the winter or summer to show extremes
seru_winter <- seru_select %>% filter(Season == "Winter")
head(seru_winter)
## # A tibble: 6 × 10
##   species        countryCode locality stateProvince latitude longitude elevation
##   <chr>          <chr>       <lgl>    <chr>            <dbl>     <dbl> <lgl>    
## 1 Selasphorus r… US          NA       Georgia           34.1     -84.6 NA       
## 2 Selasphorus r… US          NA       Texas             26.2     -98.4 NA       
## 3 Selasphorus r… US          NA       Ohio              41.4     -81.9 NA       
## 4 Selasphorus r… US          NA       Georgia           34.1     -84.6 NA       
## 5 Selasphorus r… US          NA       Texas             28.0     -97.0 NA       
## 6 Selasphorus r… US          NA       Georgia           34.0     -84.4 NA       
## # ℹ 3 more variables: eventDate <chr>, month <int>, Season <chr>
seru_summer <- seru_select %>% filter(Season == "Summer")
head(seru_summer)
## # A tibble: 6 × 10
##   species        countryCode locality stateProvince latitude longitude elevation
##   <chr>          <chr>       <lgl>    <chr>            <dbl>     <dbl> <lgl>    
## 1 Selasphorus r… US          NA       California        39.4     -120. NA       
## 2 Selasphorus r… US          NA       New Mexico        35.1     -107. NA       
## 3 Selasphorus r… US          NA       Utah              40.8     -112. NA       
## 4 Selasphorus r… US          NA       Utah              40.9     -112. NA       
## 5 Selasphorus r… US          NA       Utah              41.9     -111. NA       
## 6 Selasphorus r… US          NA       Arizona           33.4     -111. NA       
## # ℹ 3 more variables: eventDate <chr>, month <int>, Season <chr>

Now we will make the map

avg_temp_july <- raster("/Users/bellaharding/Desktop/EEB4100_R/wc2.1_10m_tavg/wc2.1_10m_tavg_07.tif")
avg_temp_dec <- raster("/Users/bellaharding/Desktop/EEB4100_R/wc2.1_10m_tavg/wc2.1_10m_tavg_12.tif")
downsampled_jul <- aggregate(avg_temp_july, fact = 4)
downsampled_dec <- aggregate(avg_temp_dec, fact = 4)

#cities chosen: Seattle, Mexico City, Denver
seattle_coords <- c(47.6061, -122.3328)
mexico_city_coords <- c(19.4326, -99.1332)
denver_coords <- c(39.7392, -104.9903)
seru_map <- leaflet::leaflet(seru_select) %>% setView(lat = 47, lng = -120, zoom = 5) %>%
  addTiles() %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap)%>%
  addProviderTiles(providers$OpenTopoMap) %>%
  addCircleMarkers(data = seru_winter, ~longitude, ~latitude, radius = 1, fillOpacity = 0.5, color = "#4446b7",
                   group = "Selasphorus rufus winter observations") %>%
  addCircleMarkers(data = seru_summer, ~longitude, ~latitude, radius =1, fillOpacity = 0.7, color = "#eaa9c8",
                   group = "Selasphorus rufus summer observations") %>%
  addLegend(position = "bottomright", 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"), opacity = 1) %>%
  addLegend(position = "bottomleft", colors = c("#4446b7", "#eaa9c8"), 
            labels = c("Selasphorus rufus winter observations", "Selasphorus rufus summer observations"), 
            opacity = 1) %>%
  addRasterImage(avg_temp_july, colors = "red", opacity = 0.7, group = "Avg. Temp. July") %>%
  addRasterImage(avg_temp_dec, colors = "blue", opacity = 0.7, group = "Avg. Temp. DEcember") %>%
  addLayersControl(
    overlayGroups = c("Selasphorus rufus winter observations",  "Selasphorus rufus summer observations", "Avg. Temp. July", "Avg. Temp. December"),
    options = layersControlOptions(collapsed = FALSE)) %>%
  addMarkers(lat = seattle_coords[1], lng = seattle_coords[2], label = "Seattle", labelOptions = labelOptions(noHide = TRUE)) %>%
  addMarkers(lat = mexico_city_coords[1], lng = mexico_city_coords[2], label = "Mexico City", labelOptions= labelOptions(noHide = TRUE)) %>%
  addMarkers(lat = denver_coords[1], lng = denver_coords[2], label = "Denver", labelOptions = labelOptions(noHide = TRUE))

seru_map

I was able to get the elevation to work just fine, but the temperature raster doesn’t seem to be loading in. Not sure why, since my code isn’t giving me any errors. I chose to label the elevation, as well as provide a legend to show when the observations of S. rufus were made.