Data Preparation for Map 1
library(terra)
## terra 1.7.65
library(tidyterra)
##
## Attaching package: 'tidyterra'
## The following object is masked from 'package:stats':
##
## filter
library(sp)
library(ggplot2)
library(ggthemes)
library(urbnmapr)
library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
setwd("/Users/briannasexton/Desktop/bing/Spring 2024/DiDa 370/NE1_50M_SR_W")
us_rast <- rast("NE1_50M_SR_W.tif")
us_rast <- terra::project(us_rast, "EPSG:32116", method = "bilinear")
##
|---------|---------|---------|---------|
=========================================
us_rast
## class : SpatRaster
## dimensions : 32498, 20166, 3 (nrow, ncol, nlyr)
## resolution : 1656.569, 1656.569 (x, y)
## extent : -16453285, 16953079, -31346642, 22488528 (xmin, xmax, ymin, ymax)
## coord. ref. : NAD83 / New York Central (EPSG:32116)
## source : spat_jeqiDoOYcW5huHH_27538.tif
## names : NE1_50M_SR_W_1, NE1_50M_SR_W_2, NE1_50M_SR_W_3
## min values : 62.21814, 89.4275, 91.50533
## max values : 255.00000, 255.0000, 255.00000
Map 1
## Map 1
states <- get_urbn_map("states", sf = TRUE) %>%
st_transform("EPSG:32116")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
rast <- crop(us_rast, states)
ggplot()+
geom_spatraster(data = rast)+
geom_sf(states, mapping = aes(), fill = 'transparent', color = "white")+
theme_map()+
scale_fill_whitebox_c(
palette = "atlas",
na.value = "white")+
labs(title = "Landcover in the United States",
fill = "Fill")+
theme(plot.title = element_text(hjust = 0.5, size = 16),
legend.position="right")
## <SpatRaster> resampled to 500864 cells for plotting
## ! `tidyterra::geom_spatraster()`: Plotting 3 overlapping layers: NE1_50M_SR_W_1, NE1_50M_SR_W_2, and NE1_50M_SR_W_3. Either:
## Use `facet_wrap(~lyr)` for faceting or
## Use `aes(fill = <name_of_layer>)` for displaying single layers
ggplot()+
geom_spatraster(data = rast)+
geom_sf(states, mapping = aes(), fill = 'transparent', color = "white")+
theme_map()+
scale_fill_whitebox_c(
palette = "atlas",
na.value = "white")+
labs(title = "Landcover in the United States",
fill = "Fill")+
theme(plot.title = element_text(hjust = 0.5, size = 16),
legend.position="right")+
xlim(-30000, 700000)+
ylim(20050, 550000)
## <SpatRaster> resampled to 500864 cells for plotting
## ! `tidyterra::geom_spatraster()`: Plotting 3 overlapping layers: NE1_50M_SR_W_1, NE1_50M_SR_W_2, and NE1_50M_SR_W_3. Either:
## Use `facet_wrap(~lyr)` for faceting or
## Use `aes(fill = <name_of_layer>)` for displaying single layers
Data Preparation For Map 2
library(leaflet)
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(urbnmapr)
library(ggplot2)
counties <- get_urbn_map("counties", sf = TRUE)
counties <- counties %>%
filter(state_abbv == "NY") %>%
st_transform("EPSG:32116")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
setwd("/Users/briannasexton/Desktop/bing/Spring 2024/DiDa 370/Homework")
expenditures <- read.csv("expenditures.csv")
library(tidyr)
##
## Attaching package: 'tidyr'
## The following object is masked from 'package:terra':
##
## extract
counties1 <- counties %>%
separate(county_name, c("county_name", "county"), sep = " County") %>%
select(-county)
expenditures_full <- counties1 %>%
left_join(expenditures,by = c("county_name" = "County"))
Map 2
expenditures.sf = st_transform(expenditures_full, "EPSG:4326")
p_popup <- paste0("Regular Day School Expenditures($): ", expenditures_full$regular_day_schools)
pal_fun <- colorQuantile("YlOrRd", NULL, n = 5)
p_popup2 <- paste0("Special Aid Fund($): ", expenditures_full$special_aid_fund)
pal_fun2 <- colorQuantile("Blues", NULL, n = 5)
leaflet() %>%
leaflet::addPolygons(data = expenditures.sf,
stroke = FALSE,
fillColor = ~pal_fun(regular_day_schools),
fillOpacity = 1,
smoothFactor = 0.5,
popup = p_popup,
group = "Regular Day Schools") %>%
leaflet::addPolygons(data = expenditures.sf,
stroke = FALSE,
fillColor = ~pal_fun2(special_aid_fund),
fillOpacity = 1,
smoothFactor = 0.5,
popup = p_popup2,
group = "Special Aid Fund") %>%
addLegend("bottomright",
pal=pal_fun,
values=expenditures.sf$regular_day_schools,
title = 'Regular Day Schools',
group = "Regular Day Schools") %>%
addLegend("bottomright",
pal=pal_fun2,
values=expenditures.sf$special_aid_fund,
title = 'Special Aid Fund',
group = "Special Aid Fund") %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addLayersControl(overlayGroups = c("Regular Day Schools", "Special Aid Fund"),
options = layersControlOptions(collapsed = FALSE))