#the stuff in the brackets suppresses errors for knitting that I get on my device (code still works regardless)
library(terra)
library(tidyverse)
library(sp)
library(ggplot2)
library(ggthemes)
library(urbnmapr)
library(sf)
library(leaflet)
#world raster
setwd("~/Documents/R Class/assignment 3")
world_rast <- rast("./NE1_50M_SR_W/NE1_50M_SR_W.tif")
#US raster
states_sf <- get_urbn_map("states", sf = TRUE)
states_sf <- st_transform(states_sf, crs = crs(world_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.
states_rast <- crop(world_rast, states_sf)
plot(states_rast)

counties <- get_urbn_map("counties", sf = TRUE)
#filter the data to get just NYS
counties <- counties %>%
filter(state_abbv == "NY") %>%
st_transform("EPSG:32116")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
housing_data <- read.csv("housing.csv")
counties <- counties %>%
separate(county_name, c("county_name", "county"), sep = " County") %>%
select(-county)
housing_data_full <- counties %>%
left_join(housing_data,by = c("county_name" = "County"))
housing.sf <- st_transform(housing_data_full, "EPSG:4326")
popup <- paste0("<strong>County: </strong>", housing_data_full$county_name,
"<br><strong>Total Units: </strong>", housing_data_full$Total.Housing.Units)
pal_fun <- colorQuantile("YlOrRd", NULL, n = 5)
popup2 <- paste0("<strong>County: </strong>", housing_data_full$county_name,
"<br><strong>Occupied Units: </strong>", housing_data_full$Occupied.Housing.Units)
pal_fun2 <- colorQuantile("Blues", NULL, n = 5)
leaflet() %>%
leaflet::addPolygons(data = housing.sf,
stroke = FALSE,
# set fill color with function from above and value
fillColor = ~pal_fun(Total.Housing.Units),
fillOpacity = 0.8,
smoothFactor = 0.5,
# add pop-up text
popup = popup,
group = 'Total Housing') %>%
leaflet::addPolygons(data = housing.sf,
stroke = FALSE,
# set fill color with function from above and value
fillColor = ~pal_fun2(Occupied.Housing.Units),
fillOpacity = 0.8,
smoothFactor = 0.5,
# add pop-up text
popup = popup2,
group = "Occupied Housing") %>%
addLegend("bottomright", # location
pal=pal_fun, # palette function
values=housing_data_full$Total.Housing.Units,
title = 'Total Housing') %>%
addLegend("bottomright", # location
pal=pal_fun2, # palette function
values=housing_data_full$Occupied.Housing.Units,
title = 'Occupied Housing') %>%
addTiles() %>%
addLayersControl(overlayGroups = c("Total Housing", "Occupied Housing"),
options = layersControlOptions(collapsed = FALSE))