library(terra)
## terra 1.7.71
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.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
setwd("/Users/clarkeddie272/Documents/DIDA 370/NE1_50M_SR_W")
us_rast <- rast("NE1_50M_SR_W.tif")
us_rast <- terra::project(us_rast, "EPSG:4326", method = "bilinear")
##
|---------|---------|---------|---------|
=========================================
#I found this projection just by googling "EPSG for United States"
library(terra)
library(tidyterra)
library(sp)
library(ggplot2)
library(ggthemes)
library(urbnmapr)
library(sf)
# I don't think were supposed to plot data on this, so I just combined the states and the raster map
setwd("/Users/clarkeddie272/Documents/DIDA 370/states")
base_map <- st_read("state_map.shp")
## Reading layer `state_map' from data source
## `/Users/clarkeddie272/Documents/DIDA 370/states/state_map.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 51 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -125.4117 ymin: 21.73154 xmax: -66.9499 ymax: 49.38436
## Geodetic CRS: WGS 84
america_rast <- crop(us_rast, base_map)
ggplot()+
geom_spatraster(data = america_rast)+
geom_sf(base_map, mapping = aes(), fill = 'transparent', color = "white")+
theme_map()+
scale_fill_whitebox_c(
# I found this palette by just hovering over the palette function
palette = "high_relief",
na.value = "grey")+
labs(title = "US MAP",
fill = "value")+
theme(plot.title = element_text(hjust = 0.5, size = 16),
legend.position = "none")
## <SpatRaster> resampled to 500636 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

#I removed the legend because I think it is unnecessary, I found out how to do this from geeksforgeeks: https://www.geeksforgeeks.org/remove-legend-in-ggplot2-in-r/#google_vignette
library(terra)
library(tidyterra)
library(sp)
library(ggplot2)
library(ggthemes)
library(urbnmapr)
library(sf)
library(leaflet)
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')`
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/clarkeddie272/Documents/DIDA 370")
education_data <- read.csv("J-34.csv")
#A huge problem I had with this data was when I tried to merge counties data and education data, all of the education data would become NA. I realized it is because the data was not numeric because it was in currency format from excel. I fixed it, took a while to realize though.
#Combine the data
library(tidyr)
##
## Attaching package: 'tidyr'
## The following object is masked from 'package:terra':
##
## extract
counties <- counties %>%
separate(county_name, c("county_name", "county"), sep = " County") %>%
select(-county)
all_data <- counties %>%
left_join(education_data, by = c("county_name" = "county_name"))
all.sf <- st_transform(all_data, "EPSG:4326")
#Creating the percentages I want to plot
all.sf <- mutate(all.sf, state_revenue_percent = (State/All.Sources1)*100)
all.sf <- mutate(all.sf, fed_revenue_percent = (Federal2/All.Sources1)*100)
e_legend <- paste0("<strong>State Funding % of School Revenue: </strong>", all.sf$state_revenue_percent)
e_legend2 <- paste0("<strong>Fed Funding % of School Revenue: </strong>", all.sf$fed_revenue_percent)
pal_use <- colorQuantile("Greens", NULL , n = 4)
pal_use2 <- colorQuantile("Blues", NULL , n = 4)
leaflet() %>%
leaflet::addPolygons(data = all.sf,
stroke = FALSE,
fillColor = ~pal_use(state_revenue_percent),
fillOpacity = 1,
smoothFactor = 0.25,
popup = e_legend,
group = "State as %") %>%
leaflet::addPolygons(data = all.sf,
stroke = FALSE,
fillColor = ~pal_use2(fed_revenue_percent),
fillOpacity = 1,
smoothFactor = 0.25,
popup = e_legend2,
group = "Fed as %") %>%
addLegend("bottomright",
pal=pal_use,
values=all.sf$state_revenue_percent,
title = 'State funding % of School Revenue',
group = "State as %") %>%
addLegend("bottomleft",
pal=pal_use2,
values=all.sf$fed_revenue_percent,
title = 'Federal funding % of School Revenue',
group = "Fed as %") %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addLayersControl(overlayGroups = c("State as %", "Fed as %"),
options = layersControlOptions(collapsed = FALSE))