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/victoriamalaspina/Documents/DIDA 370")

us_rast <- rast("NE1_50M_SR_W.tif")

us_rast
## class       : SpatRaster 
## dimensions  : 5400, 10800, 3  (nrow, ncol, nlyr)
## resolution  : 0.03333333, 0.03333333  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326) 
## source      : NE1_50M_SR_W.tif 
## colors RGB  : 1, 2, 3 
## names       : NE1_50M_SR_W_1, NE1_50M_SR_W_2, NE1_50M_SR_W_3
plot(us_rast)

country <- get_urbn_map("states", sf = TRUE)
plot(country)
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()

country <- st_transform(country, "EPSG:4326")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
usa_map <- crop(us_rast, country)
plot(usa_map)

ggplot()+
  geom_spatraster(data = usa_map)+
  theme_map()+
  scale_fill_whitebox_c(
    palette = "soft",
    na.value = "white")+
  labs(title = "Landcover in USA",
       fill = "Fill")+
  theme(plot.title = element_text(hjust = 0.5, size = 16),
        legend.position="right")
## <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

states <- get_urbn_map("states", sf = TRUE)

ggplot()+
  geom_spatraster(data = usa_map)+
  geom_sf(states, mapping = aes(), fill = 'transparent', color = "gray")+
  theme_map()+
  scale_fill_whitebox_c(
    palette = "soft",
    na.value = "white")+
  labs(title = "Landcover in the USA",
       fill = "Fill")+
  theme(plot.title = element_text(hjust = 0.5, size = 16),
        legend.position="right")
## <SpatRaster> resampled to 500636 cells for plotting
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## 
## ! `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

#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)

setwd("/Users/victoriamalaspina/Documents/DIDA 370")

ny_counties <- get_urbn_map("counties", sf = TRUE)
setwd("/Users/victoriamalaspina/Documents/DIDA 370")

death_data <- read.csv("Death_Data.csv")
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:terra':
## 
##     extract
ny_counties1 <- ny_counties %>%
  separate(county_name, c("X", "county"), sep = " County") %>%
  select(-county)
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 135 rows [5, 9, 53, 74,
## 108, 109, 148, 154, 210, 215, 235, 276, 307, 308, 385, 400, 403, 508, 557, 630,
## ...].
state_data_full <- ny_counties1 %>%
  left_join(death_data,by = c("X" = "County"))
## old-style crs object detected; please recreate object with a recent sf::st_crs()
plot(state_data_full)
## Warning: plotting the first 9 out of 17 attributes; use max.plot = 17 to plot
## all

state_data_full2 <- state_data_full %>% 
  filter(state_abbv == "NY") %>% 
  st_transform("EPSG:32116")
counties.sf = st_transform(state_data_full2, "EPSG:4326")

leaflet() %>%
  leaflet::addPolygons(data = counties.sf,
                       color = "green")
p_popup <- paste0("<strong>X85.: </strong>", state_data_full$X85.)

pal_fun <- colorBin("YlOrRd", counties.sf$X85., bins = 9)


leaflet() %>%
  leaflet::addPolygons(data = counties.sf, 
                       stroke = FALSE, 
                       fillColor = ~pal_fun(X85.), 
                       fillOpacity = 0.8, 
                       smoothFactor = 0.5, 
                       popup = p_popup)
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors
leaflet() %>%
  leaflet::addPolygons(data = counties.sf, 
                       stroke = FALSE, 
                       fillColor = ~pal_fun(X85.), 
                       fillOpacity = 0.8, 
                       smoothFactor = 0.5, 
                       popup = p_popup) %>% 
  addLegend("bottomright", 
            pal=pal_fun, 
            values=counties.sf$X85., 
            title = 'Deaths 85+ in NY 2017')
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors
leaflet() %>%
  leaflet::addPolygons(data = counties.sf, 
                       stroke = FALSE, 
                       fillColor = ~pal_fun(X85.), 
                       fillOpacity = 1, 
                       smoothFactor = 0.5, 
                       popup = p_popup) %>% 
  addLegend("bottomright",  
            pal=pal_fun,    
            values=counties.sf$X85., 
            title = 'Deaths 85+ in NYS 2017') %>% 
  addTiles()
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors
leaflet() %>%
  leaflet::addPolygons(data = counties.sf, 
                       stroke = FALSE, 
                       fillColor = ~pal_fun(X85.), 
                       fillOpacity = 1, 
                       smoothFactor = 0.5, 
                       popup = p_popup) %>% 
  addLegend("bottomright",  
            pal=pal_fun,    
            values=counties.sf$X85., 
            title = 'Deaths 85+ NYS 2017') %>% 
  addProviderTiles(providers$CartoDB.Positron)
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors
p_popup2 <- paste0("<strong> Deaths 55-64: </strong>", state_data_full2$X75.84)

pal_fun2 <- colorBin("YlOrRd", counties.sf$X55.64, bins = 10)

leaflet() %>%
  leaflet::addPolygons(data = counties.sf, 
                       stroke = FALSE,
                       fillColor = ~pal_fun(X85.), 
                       fillOpacity = 1, 
                       smoothFactor = 0.5, 
                       popup = p_popup,
                       group = "Deaths 85+") %>% 
  leaflet::addPolygons(data = counties.sf, 
                       stroke = FALSE, 
                       fillColor = ~pal_fun2(X75.84), 
                       fillOpacity = 1, 
                       smoothFactor = 0.5, 
                       popup = p_popup2,
                       group = "Deaths 75-84") %>% 
  addLegend("bottomright",  
            pal=pal_fun,    
            values=counties.sf$X85., 
            title = 'Deaths 85+',
            group = "Deaths 85+") %>% 
  addLegend("bottomright",  
            pal=pal_fun2,    
            values=counties.sf$X55.64, 
            title = 'Deaths 75-84',
            group = "Deaths 75-84") %>% 
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addLayersControl(overlayGroups = c("Deaths 85+", "Deaths 75-84"),
                   options = layersControlOptions(collapsed = FALSE))
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors
## Warning in pal_fun2(X75.84): Some values were outside the color scale and will
## be treated as NA
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette YlOrRd is 9
## Returning the palette you asked for with that many colors