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/Aiesha/Desktop/DIDA 370/tif")
ny_rast <- rast("nlcd2011_ny.tif")
ny_rast <- terra::project(ny_rast, "EPSG:32116", method = "bilinear")
##
|---------|---------|---------|---------|
=========================================
ny_rast
## class : SpatRaster
## dimensions : 21114, 25728, 1 (nrow, ncol, nlyr)
## resolution : 29.98605, 29.98605 (x, y)
## extent : -38935.85, 732545.2, 19174.79, 652300.2 (xmin, xmax, ymin, ymax)
## coord. ref. : NAD83 / New York Central (EPSG:32116)
## source : spat_1MHIEWQnPrbqeYK_48165.tif
## name : nlcd2011_ny
## min value : 11
## max value : 95
plot(ny_rast)

ggplot()+
geom_spatraster(data = ny_rast)+
theme_map()+
scale_fill_whitebox_c(
palette = "soft",
na.value = "white")+
labs(title = "Landcover in NYS",
fill = "Fill")+
theme(plot.title = element_text(hjust = 0.5, size = 16),
legend.position="right")
## <SpatRaster> resampled to 500621 cells for plotting

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()
ggplot()+
geom_spatraster(data = ny_rast)+
geom_sf(counties, mapping = aes(), fill = 'transparent', color = "gray")+
theme_map()+
scale_fill_whitebox_c(
palette = "soft",
na.value = "black")+
labs(title = "Landcover in NYS",
fill = "Fill")+
theme(plot.title = element_text(hjust = 0.5, size = 16),
legend.position="right")
## <SpatRaster> resampled to 500621 cells for plotting

ggplot()+
geom_spatraster(data = ny_rast)+
geom_sf(counties, mapping = aes(), fill = 'transparent', color = "black")+
theme_map()+
scale_fill_whitebox_c(
palette = "atlas",
na.value = "black")+
labs(title = "Landcover in NYS",
fill = "Fill")+
theme(plot.title = element_text(hjust = 0.5, size = 16),
legend.position="right")+
xlim(-30000, 700000)+
ylim(20050, 550000)
## <SpatRaster> resampled to 500621 cells for plotting

library(leaflet)
library(tidyr)
##
## Attaching package: 'tidyr'
## The following object is masked from 'package:terra':
##
## extract
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
setwd("/Users/Aiesha/Desktop/DIDA 370")
cattle <- read.csv('cattle.csv')
counties1 <- get_urbn_map("counties", sf = TRUE)
counties1 <- counties1 %>%
filter(state_abbv == "NY") %>%
st_transform("EPSG:32116")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
counties2 <- counties1 %>%
separate(county_name, c("county_name", "county"), sep = " County") %>%
dplyr::select(county_name)
cattle_full <- counties2 %>%
left_join(cattle,by = c("county_name" = "County"))
plot(cattle_full)

cattle.sf = st_transform(cattle_full, "EPSG:4326")
#https://rstudio.github.io/leaflet/colors.html
pal_fun <- colorNumeric("YlOrRd", NULL, n = 5)
pal_fun2 <- colorNumeric("Blues", NULL, n = 5)
p_popup <- paste0(cattle_full$county_name, "<strong> Milk Cows: </strong>", cattle_full$Milk.Cows)
p_popup2 <- paste0(cattle_full$county_name,"<strong> Beef Cows: </strong>", cattle_full$Beef.Cows)
leaflet() %>%
leaflet::addPolygons(
data = cattle.sf,
stroke = FALSE,
fillColor = ~ pal_fun(Milk.Cows),
fillOpacity = 1,
smoothFactor = 0.5,
popup = p_popup,
group = "Milk Cows"
) %>%
leaflet::addPolygons(
data = cattle.sf,
stroke = FALSE,
fillColor = ~ pal_fun2(Beef.Cows),
fillOpacity = 1,
smoothFactor = 0.5,
popup = p_popup2,
group = "Beef Cows"
) %>%
addLegend(
"bottomright",
pal = pal_fun,
values = cattle.sf$Milk.Cows,
title = 'Milk Cows'
) %>%
addLegend(
"bottomright",
pal = pal_fun2,
values = cattle.sf$Beef.Cows,
title = 'Beef Cows'
) %>%
#we can do this with one line of code:
addProviderTiles(providers$CartoDB.Positron) %>%
addLayersControl(
overlayGroups = c("Milk Cows", "Beef Cows"),
options = layersControlOptions(collapsed = T))