Task 1

include = FALSE
## Installing Packages
include = TRUE
require(tidyverse); require(magrittr);
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: magrittr
## 
## 
## Attaching package: 'magrittr'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     set_names
## 
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
require(sp);require(sf);require(rgdal);
## Loading required package: sp
## Loading required package: sf
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
## Loading required package: rgdal
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'rgdal'
require(classInt);require(RColorBrewer);
## Loading required package: classInt
## Loading required package: RColorBrewer
require(ggplot2);require(ggmap);require(leaflet);
## Loading required package: ggmap
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
##   Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
##   OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
## Attaching package: 'ggmap'
## 
## The following object is masked from 'package:magrittr':
## 
##     inset
## 
## Loading required package: leaflet
require(ggrepel); require(ggsn);
## Loading required package: ggrepel
## Loading required package: ggsn
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'ggsn'
library(ggpubr); library(devtools);
## Loading required package: usethis
require(htmlwidgets); require(mapview)
## Loading required package: htmlwidgets
## Loading required package: mapview
require(RColorBrewer)

Plot 1(A)

## Reading layer `NYC_COVID_Pop_by_Zip' from data source 
##   `C:\Users\ogalagedara\Documents\week 9\acsPopByZip.gpkg' using driver `GPKG'
## Simple feature collection with 180 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067113 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)

Plot 1(B)

## Plot 2
include = TRUE
COVIDDATA <- sf::st_read("C:/Users/ogalagedara/Documents/week 9/acsPopByZip.gpkg", layer="NYC_COVID_Pop_by_Zip")
## Reading layer `NYC_COVID_Pop_by_Zip' from data source 
##   `C:\Users\ogalagedara\Documents\week 9\acsPopByZip.gpkg' using driver `GPKG'
## Simple feature collection with 180 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067113 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
pal2 <- brewer.pal(9, "BuPu") ## Changing Color Brewer
plot(COVIDDATA['Positiv'],
     main = 'Total Covid Rates',
     breaks = 'quantile', nbreaks = 9,
     border = NA,
     pal = pal2,
     xlab = 'Cases',
     ylab = 'Rates',
     axes = TRUE,
     graticule = st_crs(4326))

Task 2

include = TRUE
require(classInt)
ggplot(COVIDDATA)+geom_sf(aes(fill=Positiv))

breaks_qt <- classIntervals(c(min(COVIDDATA$Positiv) - .00001,
                              COVIDDATA$Positiv), n = 7, style = "quantile")

breaks_qt
## style: quantile
##   one of 28,530,983,404 possible partitions of this variable into 7 classes
##      [13.99999,256)      [256,377.8571) [377.8571,490.8571) [490.8571,709.4286) 
##                  25                  27                  26                  25 
## [709.4286,999.7143) [999.7143,1302.143)     [1302.143,2817] 
##                  26                  26                  26
COVIDDATA <-mutate(COVIDDATA, Positiv_cat = cut(Positiv, breaks_qt$brks, dig.lab = 4, digits=1)) 
ggplot(COVIDDATA) + 
  geom_sf(aes(fill=Positiv_cat)) +
  scale_fill_brewer(palette = "BuPu", name='Cases (In Thousands)') + 
  coord_sf( ,default_crs = sf::st_crs(4326)) +
  labs(x='Longitude', y='Latitude', 
       title='Covid Positivity Rates in NYC',
       caption = 'Data Source (Us census Bureau)')

** Task 3 **

## install.packages("leaflet")
## install.packages("webshot")
library(webshot)
library(leaflet)
install.packages('sf')
## Warning: package 'sf' is in use and will not be installed
library(sf)
library(tidyverse)
library(dplyr)
COVIDDATA2 <- sf::st_read("C:/Users/ogalagedara/Documents/week 9/acsPopByZip.gpkg", layer="NYC_COVID_Pop_by_Zip")
## Reading layer `NYC_COVID_Pop_by_Zip' from data source 
##   `C:\Users\ogalagedara\Documents\week 9\acsPopByZip.gpkg' using driver `GPKG'
## Simple feature collection with 180 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067113 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
pal_positive <- colorNumeric(
                palette = 'PiYG',
                domain = COVIDDATA$Positiv
)
pal_black <- colorNumeric(
             palette = 'OrRd',
             domain = COVIDDATA$blackPp
)
positive_popup <- paste0(
              'Total Positive: ', COVIDDATA$Positiv
)
black_popup <- paste0(
  'Black Cases: ', COVIDDATA$blackPp
)

htmlMap <- leaflet(COVIDDATA%>%sf::st_transform(4326))%>%
addTiles()%>% #adding base tiles
addPolygons(
fillColor = ~pal_positive(Positiv),
fillOpacity = 0.5,
color = 'white',
weight = 1,
popup = positive_popup,
group = 'Positivity Rate'
)%>%
  addPolygons(
    fillColor = ~pal_black(blackPp),
    fillOpacity = 0.7,
    color = "black",
    weight = 1,
    popup = black_popup,
    group = "Black Cases"
  )%>%  
  addLayersControl(
    overlayGroups = c("Positivity Rate", "Black Cases"),
    options = layersControlOptions(collapsed = TRUE)
  )%>%
  addLegend(
    pal = pal_positive,
    values = ~Positiv,
    title = "Positive",
    group = "Positive"
  )
htmlMap 
htmlwidgets::saveWidget(htmlMap, 'covidmap.html', 
                        selfcontained = FALSE, libdir = 'widget-lib')
mapview::mapshot(htmlMap, 'covidmap.html')