R Markdown
#rename file
old_name <- "0021923-240216155721649.csv"
new_name <- "passerina.csv"
file.rename(old_name, new_name)
## Warning in file.rename(old_name, new_name): cannot rename file
## '0021923-240216155721649.csv' to 'passerina.csv', reason 'No such file or
## directory'
## [1] FALSE
#read csv
passerina_data <- read.csv("passerina.csv", row.names = NULL)
#it seems the column headers are concatenated
concatenated_columns <- grep("\\s|[^[:alnum:]_]", colnames(passerina_data), value = TRUE)
if(length(concatenated_columns) > 0) {
print("Concatenated column names found:")
print(concatenated_columns)
} else {
print("No concatenated column names found.")
}
## [1] "Concatenated column names found:"
## [1] "row.names"
## [2] "gbifID.datasetKey.occurrenceID.kingdom.phylum.class.order.family.genus.species.infraspecificEpithet.taxonRank.scientificName.verbatimScientificName.verbatimScientificNameAuthorship.countryCode.locality.stateProvince.occurrenceStatus.individualCount.publishingOrgKey.decimalLatitude.decimalLongitude.coordinateUncertaintyInMeters.coordinatePrecision.elevation.elevationAccuracy.depth.depthAccuracy.eventDate.day.month.year.taxonKey.speciesKey.basisOfRecord.institutionCode.collectionCode.catalogNumber.recordNumber.identifiedBy.dateIdentified.license.rightsHolder.recordedBy.typeStatus.establishmentMeans.lastInterpreted.mediaType.issue"
#it says there are concatenated column names so now im gonna separate them
library(readr)
passerina_data <- read_csv("passerina.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
## Rows: 389281 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): gbifID datasetKey occurrenceID kingdom phylum class order family ge...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
column_names <- "gbifID\tdatasetKey\toccurrenceID\tkingdom\tphylum\tclass\torder\tfamily\tgenus\tspecies\tinfraspecificEpithet\ttaxonRank\tscientificName\tverbatimScientificName\tverbatimScientificNameAuthorship\tcountryCode\tlocality\tstateProvince\toccurrenceStatus\tindividualCount\tpublishingOrgKey\tdecimalLatitude\tdecimalLongitude\tcoordinateUncertaintyInMeters\tcoordinatePrecision\televation\televationAccuracy\tdepth\tdepthAccuracy\teventDate\tday\tmonth\tyear\ttaxonKey\tspeciesKey\tbasisOfRecord\tinstitutionCode\tcollectionCode\tcatalogNumber\trecordNumber\tidentifiedBy\tdateIdentified\tlicense\trightsHolder\trecordedBy\ttypeStatus\testablishmentMeans\tlastInterpreted\tmediaType\tissue"
split_column_names <- strsplit(column_names, "\t")[[1]]
print(split_column_names)
## [1] "gbifID" "datasetKey"
## [3] "occurrenceID" "kingdom"
## [5] "phylum" "class"
## [7] "order" "family"
## [9] "genus" "species"
## [11] "infraspecificEpithet" "taxonRank"
## [13] "scientificName" "verbatimScientificName"
## [15] "verbatimScientificNameAuthorship" "countryCode"
## [17] "locality" "stateProvince"
## [19] "occurrenceStatus" "individualCount"
## [21] "publishingOrgKey" "decimalLatitude"
## [23] "decimalLongitude" "coordinateUncertaintyInMeters"
## [25] "coordinatePrecision" "elevation"
## [27] "elevationAccuracy" "depth"
## [29] "depthAccuracy" "eventDate"
## [31] "day" "month"
## [33] "year" "taxonKey"
## [35] "speciesKey" "basisOfRecord"
## [37] "institutionCode" "collectionCode"
## [39] "catalogNumber" "recordNumber"
## [41] "identifiedBy" "dateIdentified"
## [43] "license" "rightsHolder"
## [45] "recordedBy" "typeStatus"
## [47] "establishmentMeans" "lastInterpreted"
## [49] "mediaType" "issue"
#checking to see if it removed them
print(names(passerina_data))
## [1] "gbifID\tdatasetKey\toccurrenceID\tkingdom\tphylum\tclass\torder\tfamily\tgenus\tspecies\tinfraspecificEpithet\ttaxonRank\tscientificName\tverbatimScientificName\tverbatimScientificNameAuthorship\tcountryCode\tlocality\tstateProvince\toccurrenceStatus\tindividualCount\tpublishingOrgKey\tdecimalLatitude\tdecimalLongitude\tcoordinateUncertaintyInMeters\tcoordinatePrecision\televation\televationAccuracy\tdepth\tdepthAccuracy\teventDate\tday\tmonth\tyear\ttaxonKey\tspeciesKey\tbasisOfRecord\tinstitutionCode\tcollectionCode\tcatalogNumber\trecordNumber\tidentifiedBy\tdateIdentified\tlicense\trightsHolder\trecordedBy\ttypeStatus\testablishmentMeans\tlastInterpreted\tmediaType\tissue"
#it did separate the columns :D but for some reason added a "t" in front of them. i'm gonna remove them.
library(readr)
passerina_data <- read_tsv("passerina.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
## Rows: 293328 Columns: 50
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (32): datasetKey, occurrenceID, kingdom, phylum, class, order, family, ...
## dbl (13): gbifID, individualCount, decimalLatitude, decimalLongitude, coord...
## lgl (3): depth, depthAccuracy, typeStatus
## dttm (2): dateIdentified, lastInterpreted
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
colnames(passerina_data) <- gsub("\t", "", colnames(passerina_data))
print(names(passerina_data))
## [1] "gbifID" "datasetKey"
## [3] "occurrenceID" "kingdom"
## [5] "phylum" "class"
## [7] "order" "family"
## [9] "genus" "species"
## [11] "infraspecificEpithet" "taxonRank"
## [13] "scientificName" "verbatimScientificName"
## [15] "verbatimScientificNameAuthorship" "countryCode"
## [17] "locality" "stateProvince"
## [19] "occurrenceStatus" "individualCount"
## [21] "publishingOrgKey" "decimalLatitude"
## [23] "decimalLongitude" "coordinateUncertaintyInMeters"
## [25] "coordinatePrecision" "elevation"
## [27] "elevationAccuracy" "depth"
## [29] "depthAccuracy" "eventDate"
## [31] "day" "month"
## [33] "year" "taxonKey"
## [35] "speciesKey" "basisOfRecord"
## [37] "institutionCode" "collectionCode"
## [39] "catalogNumber" "recordNumber"
## [41] "identifiedBy" "dateIdentified"
## [43] "license" "rightsHolder"
## [45] "recordedBy" "typeStatus"
## [47] "establishmentMeans" "lastInterpreted"
## [49] "mediaType" "issue"
#now i wanna remove all the columns we dont need
install.packages("dplyr")
##
## The downloaded binary packages are in
## /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
passerina_data_subset <- passerina_data %>%
select(gbifID,countryCode,locality,stateProvince,occurrenceStatus,individualCount,decimalLatitude,decimalLongitude,eventDate,day,month,year,dateIdentified)
#checking
print(names(passerina_data_subset))
## [1] "gbifID" "countryCode" "locality" "stateProvince"
## [5] "occurrenceStatus" "individualCount" "decimalLatitude" "decimalLongitude"
## [9] "eventDate" "day" "month" "year"
## [13] "dateIdentified"
#for some reason there are points outside the US. I want to remove them.
library(dplyr)
passerina_data_subset_filtered <- passerina_data_subset %>%
filter(countryCode == "US")
#load leaflet
library(leaflet)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ 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
install.packages("usmap")
##
## The downloaded binary packages are in
## /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(usmap)
# Get spatial dataset of the United States
usa <- us_map(regions = "states")
#getting spatial data set for the US
library(dplyr)
library(usmap)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
usa <- us_map(regions = "states")
usa_sf <- st_as_sf(usa)
usa_sf$area_of_state <- st_area(usa_sf)
head(usa_sf)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -2590847 ymin: -2608151 xmax: 1433831 ymax: -39560.82
## Projected CRS: NAD27 / US National Atlas Equal Area
## # A tibble: 6 × 5
## fips abbr full geom area_of_state
## <chr> <chr> <chr> <MULTIPOLYGON [m]> [m^2]
## 1 02 AK Alaska (((-2396840 -2547726, -2393291 -2546396,… 390315576600.
## 2 01 AL Alabama (((1093779 -1378539, 1093270 -1374227, 1… 133957186194.
## 3 05 AR Arkansas (((483066 -927786.9, 506063 -926262.2, 5… 137791084570.
## 4 04 AZ Arizona (((-1388677 -1254586, -1389182 -1251858,… 295231686421.
## 5 06 CA California (((-1719948 -1090032, -1709613 -1090025,… 410516591657.
## 6 08 CO Colorado (((-789537.1 -678772.6, -789536.6 -67876… 269577779250.
#checking unique state values
unique_states_df <- unique(passerina_data_subset_filtered$stateProvince)
unique_states_sf <- unique(usa_sf$id)
## Warning: Unknown or uninitialised column: `id`.
print(unique_states_df)
## [1] "United States - Florida (FL)" "Texas"
## [3] "Missouri" "South Carolina"
## [5] "Louisiana" "Georgia"
## [7] "Arizona" "Florida"
## [9] "New York" "Tennessee"
## [11] "Maryland" "California"
## [13] "Oklahoma" "Illinois"
## [15] "Kansas" "Vermont"
## [17] "Iowa" NA
## [19] "Arkansas" "Ohio"
## [21] "Mississippi" "North Carolina"
## [23] "Massachusetts" "Alabama"
## [25] "Delaware" "New Hampshire"
## [27] "Michigan" "New Jersey"
## [29] "Virginia" "New Mexico"
## [31] "Oregon" "Colorado"
## [33] "Pennsylvania" "Minnesota"
## [35] "Connecticut" "Washington"
## [37] "Wyoming" "Rhode Island"
## [39] "Utah" "Maine"
## [41] "Nevada" "Wisconsin"
## [43] "North Dakota" "Kentucky"
## [45] "Nebraska" "District of Columbia"
## [47] "West Virginia" "Unknown"
## [49] "Georgia (State)" "Florida (State)"
## [51] "Texas (State)" "Tx"
## [53] "United States - Texas (TX)" "Ok"
## [55] "Frorida" "South Dakota"
## [57] "Montana" "United States - Georgia (GA)"
## [59] "Fl" "South carolina"
## [61] "Ga" "Louisiana, Captive"
## [63] "Idaho" "Indiana"
## [65] "Floride" "Louisiane"
## [67] "North carolina" "United States - New Mexico (NM)"
## [69] "Nm"
print(unique_states_sf)
## NULL
#standardizing state names
state_mapping <- c(
"Fl" = "Florida",
"Tx" = "Texas",
"Nm" = "New Mexico",
"United States - Texas (TX)" = "Texas",
"Georgia (State)" = "Georgia",
"Florida (State)" = "Florida",
"Texas (State)" = "Texas",
"Ok" = "Oklahoma",
"Frorida" = "Florida",
"United States - Georgia (GA)" = "Georgia",
"South carolina" = "South Carolina",
"Ga" = "Georgia",
"Louisiana, captive" = "Louisiana",
"Floride" = "Florida",
"Louisiane" = "Louisiana",
"North carolina" = "North Carolina",
"United States - New Mexico (NM)" = "New Mexico")
passerina_data_subset_filtered$stateProvince <- ifelse(passerina_data_subset_filtered$stateProvince %in% names(state_mapping),
state_mapping[passerina_data_subset_filtered$stateProvince],
passerina_data_subset_filtered$stateProvince)
#checking unique values again
unique_states_df <- unique(passerina_data_subset_filtered$stateProvince)
# Check unique values in the 'id' column of usa_sf
unique_states_sf <- unique(usa_sf$id)
## Warning: Unknown or uninitialised column: `id`.
# Print unique values
print(unique_states_df)
## [1] "United States - Florida (FL)" "Texas"
## [3] "Missouri" "South Carolina"
## [5] "Louisiana" "Georgia"
## [7] "Arizona" "Florida"
## [9] "New York" "Tennessee"
## [11] "Maryland" "California"
## [13] "Oklahoma" "Illinois"
## [15] "Kansas" "Vermont"
## [17] "Iowa" NA
## [19] "Arkansas" "Ohio"
## [21] "Mississippi" "North Carolina"
## [23] "Massachusetts" "Alabama"
## [25] "Delaware" "New Hampshire"
## [27] "Michigan" "New Jersey"
## [29] "Virginia" "New Mexico"
## [31] "Oregon" "Colorado"
## [33] "Pennsylvania" "Minnesota"
## [35] "Connecticut" "Washington"
## [37] "Wyoming" "Rhode Island"
## [39] "Utah" "Maine"
## [41] "Nevada" "Wisconsin"
## [43] "North Dakota" "Kentucky"
## [45] "Nebraska" "District of Columbia"
## [47] "West Virginia" "Unknown"
## [49] "South Dakota" "Montana"
## [51] "Louisiana, Captive" "Idaho"
## [53] "Indiana"
print(unique_states_sf)
## NULL
#again
passerina_data_subset_filtered <- passerina_data_subset_filtered %>%
filter(!is.na(stateProvince) & stateProvince != "Unknown")
#checking NA and unknown were removed
unique_states_df <- unique(passerina_data_subset_filtered$stateProvince)
unique_states_sf <- unique(usa_sf$id)
## Warning: Unknown or uninitialised column: `id`.
print(unique_states_df)
## [1] "United States - Florida (FL)" "Texas"
## [3] "Missouri" "South Carolina"
## [5] "Louisiana" "Georgia"
## [7] "Arizona" "Florida"
## [9] "New York" "Tennessee"
## [11] "Maryland" "California"
## [13] "Oklahoma" "Illinois"
## [15] "Kansas" "Vermont"
## [17] "Iowa" "Arkansas"
## [19] "Ohio" "Mississippi"
## [21] "North Carolina" "Massachusetts"
## [23] "Alabama" "Delaware"
## [25] "New Hampshire" "Michigan"
## [27] "New Jersey" "Virginia"
## [29] "New Mexico" "Oregon"
## [31] "Colorado" "Pennsylvania"
## [33] "Minnesota" "Connecticut"
## [35] "Washington" "Wyoming"
## [37] "Rhode Island" "Utah"
## [39] "Maine" "Nevada"
## [41] "Wisconsin" "North Dakota"
## [43] "Kentucky" "Nebraska"
## [45] "District of Columbia" "West Virginia"
## [47] "South Dakota" "Montana"
## [49] "Louisiana, Captive" "Idaho"
## [51] "Indiana"
print(unique_states_sf)
## NULL
#leaflet
map <- leaflet() %>%
addTiles() %>%
addCircleMarkers(data = passerina_data_subset_filtered,
lng = ~decimalLongitude,
lat = ~decimalLatitude,
popup = ~paste("State:", stateProvince))
## Warning in validateCoords(lng, lat, funcName): Data contains 1553 rows with
## either missing or invalid lat/lon values and will be ignored
#elevation layer
install.packages("leaflet")
##
## The downloaded binary packages are in
## /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
install.packages("raster")
##
## The downloaded binary packages are in
## /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
install.packages("elevatr")
##
## The downloaded binary packages are in
## /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(leaflet)
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
library(elevatr)
## elevatr v0.99.0 NOTE: Version 0.99.0 of 'elevatr' uses 'sf' and 'terra'. Use
## of the 'sp', 'raster', and underlying 'rgdal' packages by 'elevatr' is being
## deprecated; however, get_elev_raster continues to return a RasterLayer. This
## will be dropped in future versions, so please plan accordingly.
#bounding box
bbox <- c(left = min(passerina_data_subset_filtered$decimalLongitude),
bottom = min(passerina_data_subset_filtered$decimalLatitude),
right = max(passerina_data_subset_filtered$decimalLongitude),
top = max(passerina_data_subset_filtered$decimalLatitude))
#download elevation data for United States
elevation_data <- raster::getData("alt", country = "USA", mask = TRUE)
## Warning in raster::getData("alt", country = "USA", mask = TRUE): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
install.packages("leaflet")
##
## The downloaded binary packages are in
## /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
install.packages("raster")
##
## The downloaded binary packages are in
## /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(leaflet)
library(raster)
elevation_data1 <- raster::getData("alt", country = "USA", mask = TRUE, level = 1)
## Warning in raster::getData("alt", country = "USA", mask = TRUE, level = 1): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
elevation_data2 <- raster::getData("alt", country = "USA", mask = TRUE, level = 2)
## Warning in raster::getData("alt", country = "USA", mask = TRUE, level = 2): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
elevation_data3 <- raster::getData("alt", country = "USA", mask = TRUE, level = 3)
## Warning in raster::getData("alt", country = "USA", mask = TRUE, level = 3): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
elevation_data4 <- raster::getData("alt", country = "USA", mask = TRUE, level = 4)
## Warning in raster::getData("alt", country = "USA", mask = TRUE, level = 4): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
#merge the raster layers from raster 1
merged_raster1 <- raster::merge(elevation_data1[[1]], elevation_data2[[1]],
elevation_data3[[1]], elevation_data4[[1]])
#print the merged raster layer
print(merged_raster1)
## class : RasterLayer
## dimensions : 3012, 6948, 20927376 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -124.8, -66.9, 24.4, 49.5 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +ellps=WGS84 +no_defs
## source : memory
## names : layer
## values : -171, 4275 (min, max)
#download or generate the downsampled raster object
downsampled_raster <- raster::getData(name = "alt", country = "USA", mask = TRUE)
## Warning in raster::getData(name = "alt", country = "USA", mask = TRUE): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
library(leaflet)
library(raster)
#extract the first RasterLayer object
raster_object <- downsampled_raster[[1]]
#color palette
elevation_palette <- colorNumeric(
palette = terrain.colors(100),
domain = c(minValue(raster_object), maxValue(raster_object)),
na.color = "transparent"
)
#raster image too big, aggregate it
library(raster)
#define the factor by which to downsize the raster
factor <- 10
#aggregate the raster
downsized_raster <- aggregate(raster_object, fact = factor, fun = mean)
#legend
leaflet() %>%
addTiles() %>%
addRasterImage(downsized_raster, colors = elevation_palette) %>%
addLegend(
pal = elevation_palette,
values = c(minValue(raster_object), maxValue(raster_object)),
title = "Elevation (m)",
opacity = 0.7
)
print(object.size(downsized_raster))
## 1692448 bytes
library(raster)
#download temperature data for the contiguous United States
temperature_raster <- raster::getData(name = "worldclim", var = "bio", res = 2.5,
lon = -125, lat = 50)
## Warning in raster::getData(name = "worldclim", var = "bio", res = 2.5, lon = -125, : getData will be removed in a future version of raster
## . Please use the geodata package instead
#extract the temperature data for the contiguous United States
temperature_raster_us <- crop(temperature_raster, extent(-125, -65, 25, 50))
#gotta make this one smaller
# Define the factor by which to downsize the raster
factor <- 2
# Aggregate the raster
aggregate_temp <- aggregate(temperature_raster_us, fact = factor, fun = mean)
# Print the size of the aggregated raster
print(object.size(aggregate_temp))
## 32847296 bytes
# Add temp layer to leaflet
newtemplayer <- aggregate_temp[[1]]
# Print the size of the raster image
print(object.size(newtemplayer))
## 1741312 bytes
#visualize the downsized raster
leaflet() %>%
addTiles() %>%
addRasterImage(newtemplayer, colors = "viridis")
#adding a legend to the temperature map
if (!requireNamespace("viridis", quietly = TRUE)) {
install.packages("viridis")
}
#load viridis
library(viridis)
## Loading required package: viridisLite
#define color palette
color_palette <- viridis(100)
#temp scale legend
temperature_legend <- leaflet::colorNumeric(
palette = color_palette,
domain = values(newtemplayer),
na.color = "transparent"
)
leaflet() %>%
addTiles() %>%
addRasterImage(newtemplayer, colors = "viridis") %>%
addLegend(
pal = colorNumeric("viridis", values(newtemplayer)),
values = values(newtemplayer),
title = "Temperature (°C)",
opacity = 0.7
)
#now im gonna cluster the bird sightings so it wont make R crash if it tries to plot every single point.
#filter out rows with missing or invalid lat/lon values
passerina_data_subset_filtered <- passerina_data_subset_filtered[complete.cases(passerina_data_subset_filtered$decimalLatitude, passerina_data_subset_filtered$decimalLongitude),]
#create leaflet
map <- leaflet() %>%
addTiles()
#function to create popup content with date information
createPopupContent <- function(lat, lon, data) {
paste("Date:", data$month, "/", data$day, "/", data$year)
}
#add clustered markers with popup including date information
map <- addMarkers(
map = map,
data = passerina_data_subset_filtered,
lat = ~decimalLatitude,
lng = ~decimalLongitude,
clusterOptions = markerClusterOptions(),
popup = ~createPopupContent(decimalLatitude, decimalLongitude, data = passerina_data_subset_filtered))
#print map
map