library(terra)
## Warning: package 'terra' was built under R version 4.3.3
## terra 1.7.71
library(sp)
library(ggplot2)
library(ggthemes)
library(urbnmapr)
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
library(sf)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:terra':
##
## intersect, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(spData)
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(tidyterra)
## Warning: package 'tidyterra' was built under R version 4.3.3
##
## Attaching package: 'tidyterra'
## The following object is masked from 'package:stats':
##
## filter
library(tidyr)
##
## Attaching package: 'tidyr'
## The following object is masked from 'package:terra':
##
## extract
library(stringr)
## Warning: package 'stringr' was built under R version 4.3.3
setwd("C:/Users/matth/OneDrive/Desktop/Spatial R Class")
us_rast <- rast("./NE1_50M_SR_W/NE1_50M_SR_W.tif")
plot(us_rast)

setwd("C:/Users/matth/OneDrive/Desktop/Spatial R Class")
states_sf <- get_urbn_map(map = "states", sf = TRUE)
states_sf_transformed <- st_transform(states_sf, crs = crs(us_rast))
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## Warning in CPL_crs_from_input(x): GDAL Message 1: CRS EPSG:2163 is deprecated.
## Its non-deprecated replacement EPSG:9311 will be used instead. To use the
## original CRS, set the OSR_USE_NON_DEPRECATED configuration option to NO.
us_rast_cropped <- crop(us_rast, states_sf_transformed)
plot(us_rast_cropped)

#https://www.geeksforgeeks.org/change-column-name-of-a-given-dataframe-in-r/# I used this to help join the data
data <- read.csv("Assignment3_data.csv")
colnames(data)[1] <- "county_name"
data$county_name <- str_squish(data$county_name) #https://forum.posit.co/t/remove-spaces-for-values-in-a-column/71503/2
counties <- get_urbn_map("counties", sf = TRUE)
counties <- counties %>%
filter(state_abbv == "NY") %>%
st_transform("EPSG:4326")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
counties1 <- counties %>%
separate(county_name, c("county_name", "county"), sep = " County") %>%
select(-county)
counties_data <- left_join(counties1, data, by = "county_name")
counties_data$Unemployment..Rate.... <- as.numeric(counties_data$Unemployment..Rate....)
p_popup <- paste0("<strong>County: </strong>", counties_data$county_name,
"<br><strong>Unemployment Rate: </strong", counties_data$Unemployment..Rate....)
pal_fun <- colorNumeric(palette = "YlOrRd", domain = counties_data$Unemployment..Rate....)
p_popup2 <- paste0("<strong>% Employed: </strong>", counties_data$Employed)
pal_fun2 <- colorNumeric(palette = "blue", domain = counties_data$Employed)
leaflet() %>%
addTiles() %>%
addPolygons(data = counties_data,
stroke = FALSE,
fillColor = ~pal_fun(counties_data$Unemployment..Rate....),
fillOpacity = 1,
smoothFactor = 0.5,
popup = p_popup,
group = "Unemployment Rate") %>%
addPolygons(data = counties_data,
stroke = FALSE,
fillColor = ~pal_fun2(counties_data$Employed),
fillOpacity = 1,
smoothFactor = 0.5,
popup = p_popup2,
group = "Employed") %>%
addLegend(position = "bottomright",
pal = pal_fun,
values = counties_data$Unemployment..Rate....,
title = "Unemployment Rate",
opacity = 1) %>%
addLegend(position = "bottomright",
pal = pal_fun2,
values = counties_data$Employed,
title = "Employed",
opacity = 1) %>%
addLayersControl(overlayGroups = c("Unemployment Rate", "Employed"),
options = layersControlOptions(collapsed = FALSE))