Uploading Packages

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
## Warning: package 'sp' was built under R version 4.4.3
## Loading required package: sf
## Warning: package 'sf' was built under R version 4.4.3
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.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
## Warning: package 'classInt' was built under R version 4.4.3
## Loading required package: RColorBrewer
require(ggplot2);require(ggmap);require(leaflet);
## Loading required package: ggmap
## Warning: package 'ggmap' was built under R version 4.4.3
## ℹ 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
## Warning: package 'leaflet' was built under R version 4.4.3
require(ggrepel); require(ggsn);
## Loading required package: ggrepel
## Warning: package 'ggrepel' was built under R version 4.4.3
## 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);
## Warning: package 'ggpubr' was built under R version 4.4.3
## Warning: package 'devtools' was built under R version 4.4.3
## Loading required package: usethis
## Warning: package 'usethis' was built under R version 4.4.3
require(htmlwidgets); require(mapview)
## Loading required package: htmlwidgets
## Warning: package 'htmlwidgets' was built under R version 4.4.3
## Loading required package: mapview
## Warning: package 'mapview' was built under R version 4.4.3

Uploading data

You can also embed plots, for example:

## Reading layer `ZIP_CODE_040114' from data source 
##   `C:\Users\Kami\Downloads\Section_09\R-Spatial_I_Lab\ZIP_CODE_040114\ZIP_CODE_040114.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 263 features and 12 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
## Simple feature collection with 6 features and 23 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.99193 ymin: 40.63029 xmax: -73.78805 ymax: 40.6863
## Geodetic CRS:  WGS 84
##   ZIPCODE BLDGZIP  PO_NAME POPULATION     AREA STATE COUNTY ST_FIPS CTY_FIPS
## 1   11436       0  Jamaica      18681 22699295    NY Queens      36      081
## 2   11213       0 Brooklyn      62426 29631004    NY  Kings      36      047
## 3   11212       0 Brooklyn      83866 41972104    NY  Kings      36      047
## 4   11225       0 Brooklyn      56527 23698630    NY  Kings      36      047
## 5   11218       0 Brooklyn      72280 36868799    NY  Kings      36      047
## 6   11226       0 Brooklyn     106132 39408598    NY  Kings      36      047
##                    URL SHAPE_AREA SHAPE_LEN MODIFIED_ZCTA
## 1 http://www.usps.com/          0         0         11436
## 2 http://www.usps.com/          0         0         11213
## 3 http://www.usps.com/          0         0         11212
## 4 http://www.usps.com/          0         0         11225
## 5 http://www.usps.com/          0         0         11218
## 6 http://www.usps.com/          0         0         11226
##                                NEIGHBORHOOD_NAME BOROUGH_GROUP label
## 1                 South Jamaica/South Ozone Park        Queens 11436
## 2                           Crown Heights (East)      Brooklyn 11213
## 3                         Ocean Hill-Brownsville      Brooklyn 11212
## 4 Crown Heights (West)/Prospect Lefferts Gardens      Brooklyn 11225
## 5                     Kensington/Windsor Terrace      Brooklyn 11218
## 6             Flatbush/Prospect Lefferts Gardens      Brooklyn 11226
##   COVID_CASE_COUNT COVID_CASE_RATE POP_DENOMINATOR COVID_DEATH_COUNT
## 1             1888         9419.96        20042.54                64
## 2             5166         7996.75        64601.26               203
## 3             7182         9709.74        73966.99               330
## 4             3833         6664.50        57513.69               177
## 5             6199         8377.49        73995.92               218
## 6             7279         7476.75        97355.08               368
##   COVID_DEATH_RATE PERCENT_POSITIVE TOTAL_COVID_TESTS
## 1           319.32            17.57             11082
## 2           314.24            13.72             38560
## 3           446.14            15.64             47319
## 4           307.75            11.62             33709
## 5           294.61            13.93             45884
## 6           378.00            13.33             56287
##                         geometry
## 1 POLYGON ((-73.80585 40.6829...
## 2 POLYGON ((-73.9374 40.67973...
## 3 POLYGON ((-73.90294 40.6708...
## 4 POLYGON ((-73.95797 40.6706...
## 5 POLYGON ((-73.97208 40.6506...
## 6 POLYGON ((-73.9619 40.65487...
## Reading layer `geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a' from data source `C:\Users\Kami\Downloads\Section_09\R-Spatial_II_Lab\R-Spatial_II_Lab\2010 Census Tracts\geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2165 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
## Geodetic CRS:  WGS84(DD)
## Rows: 2167 Columns: 358
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (181): GEO_ID, NAME, DP05_0031PM, DP05_0032E, DP05_0032M, DP05_0032PE, D...
## dbl (177): Totalpop, DP05_0033M, DP05_0033PE, DP05_0034E, DP05_0034M, DP05_0...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 3985 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (27): Facility Name, Short Description, Description, Facility Open Date,...
## dbl  (9): Facility ID, Facility Phone Number, Facility Fax Number, Facility ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Reading layer `nycFoodStore' from data source 
##   `C:\Users\Kami\Downloads\Section_09\R-Spatial_II_Lab\R-Spatial_II_Lab\nycFoodStore.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 11300 features and 16 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.2484 ymin: 40.50782 xmax: -73.67061 ymax: 40.91008
## Geodetic CRS:  WGS 84

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Task 1: Static

zipcode_foods<-zipcode_merge %>%
  st_join(nyc_foods) %>%
  group_by(geometry)%>%
  mutate(n_food_stores=n())

zipcode_health<-zipcode_foods %>%
  st_join(nychealthsf) %>%
  group_by(geometry)%>%
  mutate(n_health_fac=n())

zipcode_health<-zipcode_health %>%
  filter(Description == "Hospital") %>%
  group_by(geometry)%>%
  mutate(n_hospital_fac=n())




  

ptColor <- rgb(0.1,0.9,0.1,0.1)
plot1 <-plot(zipcode_merge["COVID_DEATH_COUNT"], 
     main = "Covid Death Count", 
     breaks = "quantile",
      graticule = st_crs(4326),
     axes=TRUE,
     reset=FALSE)

popData <- merge(c_tract, acsData, by.x ='tractFIPS', by.y = 'censusCode')
ptColor <- rgb(0.1,0.9,0.1,0.1)


popData5<-st_transform(popData, 4326)
ACS_zip <- zipcode_merge %>%
  st_join(popData5) %>%
  group_by(geometry)
  
plot2<-plot(ACS_zip["elderlyPop"], 
     main = "The Elderly in NYC",
     breaks = "quantile",
     graticule = TRUE,
     axes=TRUE)

## Task 2: Using ggplot

library(scales)
## Warning: package 'scales' was built under R version 4.4.3
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
library(ggplot2)
library(sf)
plot1<- ggplot(zipcode_merge)+ 
     geom_sf(aes(fill=COVID_DEATH_COUNT)) + 
     ggtitle("Covid Death Count in NYC")+
  coord_sf(datum = NA)+
  theme(
    axis.text = element_text(size = 10),
    axis.ticks = element_line(linewidth = 0.5),
  )

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


color_palette <- colorRampPalette(c("lightblue", "darkblue"))(length(levels(ACS_zip$elderlyPop)))

plot2 <- ggplot(data = ACS_zip) + # Pass the data to ggplot()
  geom_sf(aes(fill = elderlyPop)) + # Specify the fill variable
  ggtitle("The Elderly in NYC") +
  coord_sf(datum = sf::st_crs(4326)) + # Set the CRS
  theme(
    axis.text = element_text(size = 10),
    axis.ticks = element_line(linewidth = 0.5)
  )

ggarrange(plot1, plot2, nrow = 1, ncol = 2, widths= c(6,6))

Task 3: Interactive Map

library(leaflet)
library(sf)




pal_tests <- colorNumeric(
  palette = "YlGnBu",
  domain = zipcode_merge$TOTAL_COVID_TESTS
)

pal_deaths <- colorNumeric(
  palette = "YlOrRd",
  domain = zipcode_merge$COVID_DEATH_COUNT
)


tests_popup <- paste0(
  "Total COVID Tests: ", zipcode_merge$TOTAL_COVID_TESTS
)

deaths_popup <- paste0(
  "COVID Death Count: ", zipcode_merge$COVID_DEATH_COUNT
)


htmlMap <- leaflet(zipcode_merge) %>%
  addTiles() %>% # Add base tiles
  addPolygons(
    fillColor = ~pal_tests(TOTAL_COVID_TESTS),
    fillOpacity = 0.7,
    color = "black",
    weight = 1,
    popup = tests_popup,
    group = "Total Tests"
  ) %>%
  addPolygons(
    fillColor = ~pal_deaths(COVID_DEATH_COUNT),
    fillOpacity = 0.7,
    color = "grey",
    weight = 1,
    popup = deaths_popup,
    group = "COVID Deaths"
  ) %>%
  addLayersControl(
    overlayGroups = c("Total Tests", "COVID Deaths"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  addLegend(
    pal = pal_tests,
    values = ~TOTAL_COVID_TESTS,
    title = "Total Tests",
    group = "Total Tests"
  ) %>%
  addLegend(
    pal = pal_deaths,
    values = ~COVID_DEATH_COUNT,
    title = "COVID Deaths",
    group = "COVID Deaths"
  )
htmlMap
htmlwidgets::saveWidget(htmlMap, "covid_tests_deaths_map.html")