Install all required packages

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── 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)
## Warning: package 'leaflet' was built under R version 4.2.3
library(sf)
## Warning: package 'sf' was built under R version 4.2.3
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(rgbif)
## Warning: package 'rgbif' was built under R version 4.2.3
library(raster)
## Warning: package 'raster' was built under R version 4.2.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.2.3
## 
## Attaching package: 'raster'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
library(sp)

Lauren Chance

EEB 4100 Lab 5

1. Connect with data housed in gbif to examine migration patterns of a specific species of bird.

Ruddy Turnstones are shorebirds who migrate long distance. They breed in the Arctic tundra during the summer months and migrate towards the Southern Hemisphere for the winter, specifically to the coasts of North and South America, Africa, and southern Asia (and Eurasia).

turnstone <- occ_download_get(key = "0027885-240216155721649", overwrite = TRUE, limit= 2000) %>%
  occ_download_import(turnstone_gbif_download, na.strings = c ("", NA))
## Download file size: 173.89 MB
## On disk at C:\Users\Lauren\OneDrive\Documents\eeb 4100\0027885-240216155721649.zip
unique(turnstone$countryCode)
##   [1] "KR" "AU" "NL" "NA" "GB" "FI" "US" "NO" "CA" "KY" "FR" "AW" "PT" "EC" "ES"
##  [16] "SV" "HK" "BQ" "AX" "DK" "UA" "CR" "MX" "UM" "PR" "PE" "DE" "IN" "JM" "BZ"
##  [31] "BS" "IM" "RU" "SA" "IT" "ZA" "TZ" "NF" "CO" "NI" "PH" "MG" "KE" "CU" "CL"
##  [46] "BE" "CN" "AR" "DO" "JP" "IS" "NC" "IE" "SN" "CV" "GH" "SE" "GM" "SR" "MA"
##  [61] "TH" "RO" "SX" "GR" "GN" "SJ" "BJ" "UZ" "BR" "GF" "LC" "GT" "TW" "UG" "NZ"
##  [76] "HN" "SG" "VC" "PA" "KN" "BM" "GA" "CH" "AG" "QA" "MZ" "FJ" "TC" "UY" "LT"
##  [91] "DZ" "IR" "LK" "GP" "ID" "BY" "EH" "MH" "TT" "VI" "MR" "AT" "HU" "SC" "VE"
## [106] "KI" "ZZ" "TK" "CY" "MQ" "MU" "CW" "OM" "AS" "CZ" "MF" "PW" "HT" "YT" "RE"
## [121] "KM" "MY" "IL" "GU" "PL" "EE" "IO" "CC" "NG" "TR" "BB" "MN" "SI" "PG" "GL"
## [136] "TV" "GE" "AE" "KW" "PM" "FO" "TF" "PF" "BL" "WF" "TO" "WS" "AI" "LV" "LY"
## [151] "VU" "ZW" "FM" "JE" "HR" "TN" "ST" "MT" "MP" "PY" "KZ" "MV" "LR" "VG" "SK"
## [166] "LB" "GD" "GY" "TL" "BG" "BH" "KG" "EG" "BD" "DJ" "SO" "GW" "RS" "LA" "GG"
## [181] "DM" "MM" "SB" "KH" "GI" "CX" "AO" "YE" "SH" "AZ" "PK" "SL" "VN" "AL" "ME"
## [196] "NR" "FK" "CK" "ER" "MS" "MD" "MO" "NU" "CI" "ET" "CG" "IQ" "SD" "BW" "MW"
## [211] "ZM" "BI" "CD" "BO" "LU" "TJ" "JO" "AM" "KP" "CM" "BA" "SZ" "MK" "XK" "CF"
## [226] "NP" "TD" "RW"

Migration patterns

Turnstones spend the breeding season in the Arctic (North America, Europe, Asia) and migrate to the Southern Hemisphere in the winter.

turnstone_summer <- turnstone %>%
  filter(countryCode %in% c("EU", "SI", "CA", "GL", "RU")) %>%
  sample_n(size=2000)

turnstone_winter <- turnstone %>%
  filter(countryCode %in% c("US", "SA", "AF", "CN", "TH", "AU")) %>%
  sample_n(size=2000)

2. Display this information (bird observations and migration) on a map via leaflet-include layers for temperature and elevation.

temp <- getData(name="worldclim", var ="tmax", res = 10)
## Warning in getData(name = "worldclim", var = "tmax", res = 10): getData will be removed in a future version of raster
## . Please use the geodata package instead
temp_raster <- temp$tmax6
gain(temp_raster) <- 0.1
map <- leaflet() %>% setView(lng = -79, lat = 37.8, zoom = 5) %>% addTiles() %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>% 
  addProviderTiles(providers$OpenTopoMap) %>%
  addCircleMarkers(data = turnstone_summer,
                   ~decimalLongitude, ~decimalLatitude,
                   color="magenta", fillOpacity = 1, radius =3, popup = ~paste(scientificName, "<br>", eventDate), group = "Arenaria interpres summer observations") %>%
  addCircleMarkers(data = turnstone_winter,
                   ~decimalLongitude, ~decimalLatitude,
                   color="teal", fillOpacity = 1, radius =3, popup = ~paste(scientificName, "<br>", eventDate), group = "Arenaria interpres winter observations") %>% addRasterImage(temp_raster, colors = "lightblue", opacity = 0.7, group = "Temperature") %>%
  addLayersControl(
    overlayGroups = c("Arenaria interpres summer observations", "Arenaria interpres winter observations", "Temperature"),
    options = layersControlOptions(collapsed=FALSE)) %>%
  addLegend(position = "bottomleft",
            colors =c("magenta", "teal", "lightblue"),
            labels = c("Arenaria interpres summer observations", "Arenaria interpres winter observations", "Temperature"), opacity=1) %>%
  addLegend("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)
## Warning in validateCoords(lng, lat, funcName): Data contains 3 rows with either
## missing or invalid lat/lon values and will be ignored
## Warning in validateCoords(lng, lat, funcName): Data contains 5 rows with either
## missing or invalid lat/lon values and will be ignored
map

3. Label the three most populous cities near the largest set of observations for your target species.

The three cities I am using are New York City, Toronto, Canada, and Jacksonville, Florida.

# Latitude and Longitude for NYC
nyc_coords <- c(40.7306, -73.9352)
tor_coords <- c(43.6511, -79.3470)
jac_coords <- c(30.4902, -81.6473)
map <- leaflet() %>% setView(lng = -79, lat = 37.8, zoom = 5) %>% addTiles() %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>% 
  addProviderTiles(providers$OpenTopoMap) %>%
  addCircleMarkers(data = turnstone_summer,
                   ~decimalLongitude, ~decimalLatitude,
                   color="magenta", fillOpacity = 1, radius =3, popup = ~paste(scientificName, "<br>", eventDate), group = "Arenaria interpres summer observations") %>%
  addCircleMarkers(data = turnstone_winter,
                   ~decimalLongitude, ~decimalLatitude,
                   color="teal", fillOpacity = 1, radius =3, popup = ~paste(scientificName, "<br>", eventDate), group = "Arenaria interpres winter observations") %>% addRasterImage(temp_raster, colors = "lightblue", opacity = 0.7, group = "Temperature") %>%
  addLayersControl(
    overlayGroups = c("Arenaria interpres summer observations", "Arenaria interpres winter observations", "Temperature"),
    options = layersControlOptions(collapsed=FALSE)) %>%
  addLegend(position = "bottomleft",
            colors =c("magenta", "teal", "lightblue"),
            labels = c("Arenaria interpres summer observations", "Arenaria interpres winter observations", "Temperature"), opacity=1) %>%
  addLegend("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) %>%
  addMarkers(lng = nyc_coords[2], lat = nyc_coords[1], label = "New York City", labelOptions = labelOptions(noHide = TRUE)) %>%
  addMarkers(lng = tor_coords[2], lat = tor_coords[1], label = "Toronto", labelOptions = labelOptions(noHide =TRUE)) %>%
  addMarkers(lng = jac_coords[2], lat = jac_coords[1], label = "Jacksonville", labelOptions = labelOptions(noHide = TRUE))
## Warning in validateCoords(lng, lat, funcName): Data contains 3 rows with either
## missing or invalid lat/lon values and will be ignored
## Warning in validateCoords(lng, lat, funcName): Data contains 5 rows with either
## missing or invalid lat/lon values and will be ignored
map

4. Provide appropriate legends to describe the data.

I have added legends to describe the data in #2. I added a legend overlay to check or uncheck summer/winter to show where the birds are primarily during breeding season and migration separately, and I also added a overlay legend for temperature, because this allows me to take away the temperature overlay and view the elevation; there is also a key for elevation for clarity.

I also added a measuring tool here to help viewers further analyze the data and see the distance that the birds have migrated. I gave the temperature overlay a gradient and a legend to show that they migrate to warmer weather in the winter.

pal <- colorNumeric(c("purple", "blue", "green", "yellow", "orange", "red"), domain = c(-20, 40))
map <- leaflet() %>% setView(lng = -79, lat = 37.8, zoom = 5) %>% addTiles() %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>% 
  addProviderTiles(providers$OpenTopoMap) %>%
  addCircleMarkers(data = turnstone_summer,
                   ~decimalLongitude, ~decimalLatitude,
                   color="magenta", fillOpacity = 1, radius =3, popup = ~paste(scientificName, "<br>", eventDate), group = "Arenaria interpres summer observations") %>%
  addCircleMarkers(data = turnstone_winter,
                   ~decimalLongitude, ~decimalLatitude,
                   color="teal", fillOpacity = 1, radius =3, popup = ~paste(scientificName, "<br>", eventDate), group = "Arenaria interpres winter observations") %>% addRasterImage(temp_raster, colors = pal, opacity = 0.7, group = "Temperature") %>%
  addLegend(pal = pal, values = c(-20, 40), title = "Temperature in Celsius") %>%
  addLayersControl(
    overlayGroups = c("Arenaria interpres summer observations", "Arenaria interpres winter observations", "Temperature"),
    options = layersControlOptions(collapsed=FALSE)) %>%
  addLegend(position = "bottomleft",
            colors =c("magenta", "teal", pal),
            labels = c("Arenaria interpres summer observations", "Arenaria interpres winter observations", "Temperature"), opacity=1) %>%
  addLegend("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) %>%
  addMarkers(lng = nyc_coords[2], lat = nyc_coords[1], label = "New York City", labelOptions = labelOptions(noHide = TRUE)) %>%
  addMarkers(lng = tor_coords[2], lat = tor_coords[1], label = "Toronto", labelOptions = labelOptions(noHide =TRUE)) %>%
  addMarkers(lng = jac_coords[2], lat = jac_coords[1], label = "Jacksonville", labelOptions = labelOptions(noHide = TRUE)) %>%
  addMeasure(position = "topleft")
## Warning in colors(.): Some values were outside the color scale and will be
## treated as NA
## Warning in validateCoords(lng, lat, funcName): Data contains 3 rows with either
## missing or invalid lat/lon values and will be ignored
## Warning in validateCoords(lng, lat, funcName): Data contains 5 rows with either
## missing or invalid lat/lon values and will be ignored
map