library(tidyverse)
## Warning: package 'dplyr' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” dplyr 1.1.4 âś” readr 2.1.4
## âś” forcats 1.0.0 âś” stringr 1.5.0
## âś” ggplot2 3.4.3 âś” tibble 3.2.1
## âś” lubridate 1.9.2 âś” tidyr 1.3.0
## âś” 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
#install.packages("leaflet")
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.2
#install.packages("sf")
library(sf)
## Warning: package 'sf' was built under R version 4.3.2
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
#install.packages("rgbif")
library(rgbif)
## Warning: package 'rgbif' was built under R version 4.3.2
library(dplyr)
library(shiny)
american_woodcock_gbif <- occ_download_get(key = "0001262-240229165702484", overwrite = TRUE) %>%
occ_download_import(american_woodcock_gbif_download, na.strings = c("", NA)) %>%
filter(decimalLatitude != "NA") %>%
filter(month != "NA") %>%
filter(year <= "2000")
## Download file size: 96.07 MB
## On disk at C:\Users\Max\OneDrive - University of Connecticut\EEB 4100\Lab 5\0001262-240229165702484.zip
american_woodcock_gbif <- american_woodcock_gbif %>% select_if(~ !any(is.na(.))) #gets rid of all the columns that contain NA
colnames(american_woodcock_gbif) #shows which columns we have in the new dataframe
## [1] "gbifID" "license" "publisher"
## [4] "basisOfRecord" "occurrenceStatus" "eventDate"
## [7] "year" "month" "countryCode"
## [10] "decimalLatitude" "decimalLongitude" "acceptedNameUsageID"
## [13] "scientificName" "kingdom" "phylum"
## [16] "class" "order" "family"
## [19] "genus" "taxonRank" "taxonomicStatus"
## [22] "datasetKey" "publishingCountry" "lastInterpreted"
## [25] "hasCoordinate" "hasGeospatialIssues" "taxonKey"
## [28] "acceptedTaxonKey" "kingdomKey" "phylumKey"
## [31] "classKey" "orderKey" "familyKey"
## [34] "genusKey" "speciesKey" "species"
## [37] "acceptedScientificName" "protocol" "lastParsed"
## [40] "lastCrawled" "projectId" "isSequenced"
american_woodcock_occurence <- dplyr::rename(american_woodcock_gbif, latitude = decimalLatitude, longitude = decimalLongitude) #rename the longitude and latitude columns
american_woodcock_occurence <- american_woodcock_occurence %>%
filter(countryCode == "US")
american_woodcock_summer <- american_woodcock_occurence %>% filter(month %in% (3:9)) #Summer month Data
american_woodcock_winter <- american_woodcock_occurence %>% filter(!(month %in% (3:9))) # Winter Month Occurences
w <- leaflet::leaflet(american_woodcock_occurence) %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addCircleMarkers(~longitude, ~latitude, popup = american_woodcock_occurence$species, radius = 1, weight = 2, opacity = 0.5, fill = TRUE, fillOpacity = 0.2, fillColor = "darkorchid")
w
#install.packages(c("raster","ggplot2", "magrittr", "sf"))
remotes::install_github("wmgeolab/rgeoboundaries")
## Skipping install of 'rgeoboundaries' from a github remote, the SHA1 (ecb0269e) has not changed since last install.
## Use `force = TRUE` to force installation
library(raster)
## Warning: package 'raster' was built under R version 4.3.2
## Loading required package: sp
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
## (status 2 uses the sf package in place of rgdal)
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
library(ggplot2)
library(magrittr)
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:raster':
##
## extract
## The following object is masked from 'package:purrr':
##
## set_names
## The following object is masked from 'package:tidyr':
##
## extract
library(sf)
library(rgeoboundaries)
## Registered S3 method overwritten by 'hoardr':
## method from
## print.cache_info httr
#getting the data
tmax_data <- 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
# Convert to celsius
gain(tmax_data) <- 0.1
# Calculating mean of the monthly maximum temperatures
tmax_mean <- mean(tmax_data)
# Downloading the boundary of US
US_sf <- geoboundaries("United States of America")
# Extracting temperature data of US
tmax_mean_US <- raster::mask(tmax_mean, as_Spatial(US_sf))
# Converting the raster object into a dataframe
tmax_mean_US_df <- as.data.frame(tmax_mean_US, xy = TRUE, na.rm = TRUE)
tmax_mean_US_df %>%
ggplot(aes(x = x, y = y)) +
geom_raster(aes(fill = layer)) +
geom_sf(data = US_sf, inherit.aes = FALSE, fill = NA) +
labs(
title = "Mean Monthly Temps for US"
) +
xlab("Longitude") +
ylab("Latitude") +
scale_fill_gradient(
name = "Temperature (°C)",
low = "#FEED99",
high = "#AF3301"
)
## Warning: Raster pixels are placed at uneven horizontal intervals and will be shifted
## ℹ Consider using `geom_tile()` instead.
## Warning: Raster pixels are placed at uneven horizontal intervals and will be shifted
## ℹ Consider using `geom_tile()` instead.
#getting the data
tmax_data <- 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
# Converting temperature values to Celcius
gain(tmax_data) <- 0.1
# Calculating mean of the monthly maximum temperatures
tmax_mean <- mean(tmax_data)
# Downloading the boundary of US
US_sf <- geoboundaries("United States of America")
# Extracting temperature data of US
tmax_mean_US <- raster::mask(tmax_mean, as_Spatial(US_sf))
map <- leaflet() %>% setView(lng = -79, lat = 37.8, zoom = 5) %>% addTiles() %>%
addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
addProviderTiles(providers$OpenTopoMap) %>%
addCircleMarkers(data = american_woodcock_summer,
~longitude, ~latitude,
color = "#2ca25f", fillOpacity = .5, radius = 3,
popup = ~paste(scientificName, "<br>", eventDate),
group = "American Woodcock Summer Observations") %>%
addCircleMarkers(data = american_woodcock_winter,
~longitude, ~latitude,
color = "#c51b8a", fillOpacity = .2, radius = 3,
popup = ~paste(scientificName, "<br>", eventDate),
group = "American Woodcock Winter Observations") %>%
addRasterImage(tmax_mean_US, colors = "cadetblue1", opacity = 0.7, group = "Temperature") %>%
addLayersControl(
overlayGroups = c("American Woodcock Summer Observations", "American Woodcock Winter Observations", "Temperature"),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(position = "bottomright",
colors = c("#2ca25f", "#c51b8a", "cadetblue1"),
labels = c("American Woodcock Summer Observations", "American Woodcock Winter Observations", "Temperature"),
opacity = 1) %>%
addLegend("bottomleft", title = "Elevation",
pal = colorNumeric(palette = c("#8c510a", "#d8b365", "#f6e8c3", "#c7eae5", "#5ab4ac", "#01665e"), 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("bottomright", title = "Temperature",
pal = colorNumeric(palette = c("#2166ac", "#67a9cf", "#d1e5f0", "#fddbc7", "#ef8a62", "#b2182b"), domain = c(0,60)),
values = c(0, 60),
labels = c("0-10°C", "11-20°C", "21-30°C", "31-40°C", "41-50°C", "51-60°C"),opacity = 1)###Add simulated temperature - Issabelle's code
map
This map may look blocky, but I have finally added temperature and elevation to my map of American woodcock occurances.
The three largest cities with the most observations are New York, Boston, and Detroit
#New York Coords
nyc_coords <- c(40.7128, -74.0060)
#Boston Coords
boston_coords <- c(42.3601, -71.0589)
#Detroit Coords
detroit_coords <- c(42.3314, -83.0458)
empireIcon <- makeIcon(iconUrl = "empire.jpg",
iconWidth = 30,
iconHeight = 50,
iconAnchorX = 20,
iconAnchorY = 20)
detroitIcon <- makeIcon(iconUrl = "detroit.png",
iconWidth = 60,
iconHeight = 50,
iconAnchorX = 20,
iconAnchorY = 20)
celticsIcon <- makeIcon(iconUrl = "celtics.png",
iconWidth = 40,
iconHeight = 40,
iconAnchorX = 20,
iconAnchorY = 20)
map <- leaflet() %>% setView(lng = -79, lat = 37.8, zoom = 5) %>% addTiles() %>%
addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
addProviderTiles(providers$OpenTopoMap) %>%
addCircleMarkers(data = american_woodcock_summer,
~longitude, ~latitude,
color = "#2ca25f", fillOpacity = .5, radius = 3,
popup = ~paste(scientificName, "<br>", eventDate),
group = "American Woodcock Summer Observations") %>%
addCircleMarkers(data = american_woodcock_winter,
~longitude, ~latitude,
color = "#c51b8a", fillOpacity = .2, radius = 3,
popup = ~paste(scientificName, "<br>", eventDate),
group = "American Woodcock Winter Observations") %>%
addRasterImage(tmax_mean_US, colors = "cadetblue1", opacity = 0.7, group = "Temperature") %>%
addLayersControl(
overlayGroups = c("American Woodcock Summer Observations", "American Woodcock Winter Observations", "Temperature"),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(position = "bottomright",
colors = c("#2ca25f", "#c51b8a", "cadetblue1"),
labels = c("American Woodcock Summer Observations", "American Woodcock Winter Observations", "Temperature"),
opacity = 1) %>%
addLegend("bottomleft", title = "Elevation",
pal = colorNumeric(palette = c("#8c510a", "#d8b365", "#f6e8c3", "#c7eae5", "#5ab4ac", "#01665e"), 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("bottomright", title = "Temperature",
pal = colorNumeric(palette = c("#2166ac", "#67a9cf", "#d1e5f0", "#fddbc7", "#ef8a62", "#b2182b"), domain = c(0,60)),
values = c(0, 60),
labels = c("0-10°C", "11-20°C", "21-30°C", "31-40°C", "41-50°C", "51-60°C"),opacity = 1) %>% ###Add simulated temperature - Issabelle's code
addMarkers(lng=nyc_coords[2], lat = nyc_coords[1], label = "New York City", icon = empireIcon) %>%
addMarkers(lng=boston_coords[2], lat = boston_coords[1], label = "Boston", icon = celticsIcon) %>%
addMarkers(lng=detroit_coords[2], lat = detroit_coords[1], label = "Detroit", icon = detroitIcon)
map
With this map, all we needed to do was add the coordinates of the populous cities and add markers to point them out.
Since there birds are migratory, I wanted to show that by splitting the observations into two different data sets, one for their winter observations and one for their summer observations. For all observations before 2000, we see that there are a lot more of the summer observations than the winter ones. However, with this interactive map, we would be able to take take off the data sets we don’t want to see and we will notice a shift in the density of observations on the map with more of the winter observations being in the south while the summer observations are happening more in the north. I also added in the empire state building for New york becuase it is easily recognizable. I also like basketball for so both Boston and Detroit, I added in both of their team logos (Detroit sucks right now).