Don’t use a single chunk for the entire assignment. Break it into multiple chunks.
You can name the code chunk and also set options.
#read ACS data
acsCovidDat <- sf::st_read('./acsPopByZip.gpkg', layer="NYC_COVID_Pop_by_Zip")
## Reading layer `NYC_COVID_Pop_by_Zip' from data source
## `/Users/jiadavalenza/Desktop/spring 2025/GTECH 385/Spatial R/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)
#plotting the entire data frame, gives 9 graphs
plot(acsCovidDat)
## Warning: plotting the first 9 out of 13 attributes; use max.plot = 13 to plot all
#STATIC MAP 1: plotting just the COVID19 data, measured through positive tests, on a static map
plot(acsCovidDat["Positiv"], main = "Positive COVID19 Cases By Zipcode", breaks = "quantile", border = NA, graticule = TRUE)
#STATIC MAP 2: plotting elderly population by Zipcode
plot(acsCovidDat["eldrlyP"], main = "Elderly Population by Zipcode", breaks = "quantile", border = NA, graticule = TRUE)
##Task 2: Using ggplot demonstrate the connection between positive COVID cases and the elderly population
#Installing the patchwork package
library(patchwork)
posCOVID <- ggplot(acsCovidDat) +
geom_sf(aes(fill=Positiv), show.legend = TRUE) + geom_sf_label(data = acsCovidDat %>% dplyr::filter(Positiv > 1900),
aes(label = Positiv),
label.size = .04,
size = 2) +
scale_colour_gradient(name = 'Number of Positive Cases',
low = "#fdc4c4",
high = "#e10404",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "fill") +
coord_sf(xlim = c(-74.30, -73.70), ylim=c(40.40, 40.95), default_crs = sf::st_crs(4326) ) + scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) +
labs(x='Longitude', y='Latitude',
title='Positive COVID19 Cases by Zipcode',
caption = 'Data Source: ACS')+
theme(legend.position = "bottom");
eldpop <- ggplot(acsCovidDat) +
geom_sf(aes(fill=eldrlyP), show.legend = TRUE) + geom_sf_label(data = acsCovidDat %>% dplyr::filter(eldrlyP > 14000),
aes(label = eldrlyP),
label.size = .04,
size = 2) + scale_colour_gradient(name = 'Total Elderly Population',
low = "#c4c8fd",
high = "#0413e3",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "fill") +
coord_sf(xlim = c(-74.30, -73.70), ylim=c(40.40, 40.95), default_crs = sf::st_crs(4326) ) + scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) +
labs(x='Longitude', y='Latitude',
title='Elderly Population by Zipcode',
caption = 'Data Source: ACS')+
theme(legend.position = "bottom");
posCOVID + eldpop + plot_layout(ncol = 2, nrow = 1, widths=c(12,12))
require(classInt)
library(classInt)
library(leaflet)
st_crs(acsCovidDat)
## Coordinate Reference System:
## User input: NAD83 / New York Long Island (ftUS)
## wkt:
## PROJCRS["NAD83 / New York Long Island (ftUS)",
## BASEGEOGCRS["NAD83",
## DATUM["North American Datum 1983",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4269]],
## CONVERSION["SPCS83 New York Long Island zone (US survey foot)",
## METHOD["Lambert Conic Conformal (2SP)",
## ID["EPSG",9802]],
## PARAMETER["Latitude of false origin",40.1666666666667,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8821]],
## PARAMETER["Longitude of false origin",-74,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8822]],
## PARAMETER["Latitude of 1st standard parallel",41.0333333333333,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8823]],
## PARAMETER["Latitude of 2nd standard parallel",40.6666666666667,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8824]],
## PARAMETER["Easting at false origin",984250,
## LENGTHUNIT["US survey foot",0.304800609601219],
## ID["EPSG",8826]],
## PARAMETER["Northing at false origin",0,
## LENGTHUNIT["US survey foot",0.304800609601219],
## ID["EPSG",8827]]],
## CS[Cartesian,2],
## AXIS["easting (X)",east,
## ORDER[1],
## LENGTHUNIT["US survey foot",0.304800609601219]],
## AXIS["northing (Y)",north,
## ORDER[2],
## LENGTHUNIT["US survey foot",0.304800609601219]],
## USAGE[
## SCOPE["Engineering survey, topographic mapping."],
## AREA["United States (USA) - New York - counties of Bronx; Kings; Nassau; New York; Queens; Richmond; Suffolk."],
## BBOX[40.47,-74.26,41.3,-71.8]],
## ID["EPSG",2263]]
#Re-projecting before using leaflet
acsCovidDat <- st_transform(acsCovidDat, 4326)
leaflet(acsCovidDat) %>%
addPolygons()
pal_fun <- colorQuantile("YlOrRd", NULL, n = 5)
p_tip <- paste0("GEOID: ", acsCovidDat$GEOID);
info_popup <- paste0("<strong> Zipcode: </strong>",acsCovidDat$ZIPCODE,
" <br/>", "<strong> Elderly Population: </strong>",acsCovidDat$eldrlyP,
" <br/>","<strong> Positive Covid-19 Cases: </strong>",
acsCovidDat$Positiv%>%round(3)%>%format(nsmall = 3),
sep="")
#This stage sets me up for not having percentages in my legend, in the next step
breaks_qt <- classIntervals(acsCovidDat$Positiv, n = 7, style = "quantile")
htmlMap <- leaflet(acsCovidDat) %>%
addPolygons(
stroke = FALSE, # remove polygon borders
fillColor = ~pal_fun(Positiv), # set fill color with function from above and value
fillOpacity = 0.8, smoothFactor = 0.5, # make it nicer
popup = info_popup) %>%
addTiles() %>%
addLegend("bottomright", colors = brewer.pal(7, "YlOrRd"),
labels = paste0("up to ", format(breaks_qt$brks[-1], digits = 2)),
title = 'Positive COVID-19 Cases by Zipcode') # legend title
htmlMap
#saving the map as an html
htmlwidgets::saveWidget(htmlMap, 'posCOVID_widget.html')
install.packages("knitr")
## Error in install.packages : Updating loaded packages
library(knitr)
install.packages("rmarkdown")
## Error in install.packages : Updating loaded packages
library(rmarkdown)