Update the title with your information. Make sure to include identification information so that we know it is your submission.
Also update the author name and date accordingly.
Check out the Source Code from the top-right corner
</>Code
menu.
In the following R code chunk, load_packages
is the code
chunk name. include=FALSE
suggests that the code chunk will
run, but the code itself and its outputs will not be included in the
rendered HTML. echo=TRUE
in the following code chunk
suggests that the code and results from running the code will be
included in the rendered HTML.
Don’t use a single chunk for the entire assignment. Break it into multiple chunks.
nyc_covid_data_testing <- read_csv("C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/Section_08/R-Spatial_II_Lab/R-Spatial_II_Lab/tests-by-zcta_2020_04_12.csv")
## Rows: 178 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (4): MODZCTA, Positive, Total, zcta_cum.perc_pos
##
## ℹ 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.
nyc_postal <- st_read("C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source
## `C:\Users\dwvil\Documents\SPRING 2025\R LANGUAGE\R-Spatial\R-Spatial\Data\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)
NYS_Health_Facility <- read_csv("C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/R-Spatial_I_Lab/NYS_Health_Facility.csv")
## Rows: 3990 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (28): Facility Name, Short Description, Description, Facility Open Date,...
## dbl (8): 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.
NYS_Retail_Food_Stores <- read_csv(
"C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/R-Spatial_I_Lab/nys_retail_food_Store_xy.csv",
locale = locale(encoding = "Latin1")
)
## Rows: 29389 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): ï..County, Operation.Type, Establishment.Type, Entity.Name, DBA.Na...
## dbl (4): License.Number, Zip.Code, Y, X
## num (1): Square.Footage
## lgl (2): Address.Line.2, Address.Line.3
##
## ℹ 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.
nyc_covid_data_testing <- nyc_covid_data_testing %>%
mutate(MODZCTA = as.character(MODZCTA))
nyc_postal_Joined <- nyc_postal %>%
left_join(nyc_covid_data_testing, by = c("ZIPCODE" = "MODZCTA")) # Fixed missing parenthesis
#task 1
library(ggplot2)
library(sf)
# Plot COVID-19 positive test rate
ggplot(nyc_postal_Joined) +
geom_sf(aes(fill = Positive / Total), color = NA) +
scale_fill_viridis_c(option = "magma", name = "Positive Rate") +
labs(title = "NYC COVID-19 Positive Test Rate (April 2020)") +
theme_minimal() +
theme(axis.text = element_blank()) +
coord_sf(datum = st_crs(4326)) # Add graticule
Quarto markdown is different from R markdown in terms of chunk options. See chunk options at Quarto website.
#task 2
nycNursingHome <- NYS_Health_Facility %>%
dplyr::filter(`Short Description` == "NH") %>%
dplyr::group_by(`Facility Zip Code`)
nyc_postal_nursing_homes <- nyc_postal %>%
left_join(nycNursingHome, by = c("ZIPCODE" = "Facility Zip Code"))
## Warning in sf_column %in% names(g): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 6 of `x` matches multiple rows in `y`.
## ℹ Row 6 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
## "many-to-many"` to silence this warning.
plot(nyc_postal_nursing_homes["Short Description"],
main = "Nursing Homes in NYC ZIP Codes",
pal = c("red"),
key.pos = 1,
graticule = TRUE)
library(tmap)
## Warning: package 'tmap' was built under R version 4.4.3
library(sf)
# First, calculate positive rate as a new column
nyc_postal_Joined <- nyc_postal_Joined %>%
mutate(Positive_Rate = (Positive / Total) * 100)
# Set tmap to interactive mode
tmap_mode("view")
## ℹ tmap mode set to "view".
# Create interactive map
nyc_covid_map <- tm_shape(nyc_postal_Joined) +
tm_polygons(
col = "Positive", # Color by case count
palette = "YlOrRd",
style = "quantile",
n = 5,
alpha = 0.7,
border.col = "white",
id = "ZIPCODE", # Show ZIP code on hover
popup.vars = c(
"COVID-19 Cases" = "Positive",
"Total Tests" = "Total",
"Positive Rate (%)" = "Positive_Rate" # Use pre-calculated column
),
title = "COVID-19 Cases (April 2020)"
) +
tm_basemap("OpenStreetMap") +
tm_view(bbox = st_bbox(nyc_postal_Joined)) # Force NYC view
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "quantile"`, use fill.scale =
## `tm_scale_intervals()`.
## ℹ Migrate the argument(s) 'style', 'n', 'palette' (rename to 'values') to
## 'tm_scale_intervals(<HERE>)'[v3->v4] `tm_polygons()`: use 'fill' for the fill color of polygons/symbols
## (instead of 'col'), and 'col' for the outlines (instead of 'border.col').[v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'
# Display and save
nyc_covid_map
## [cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "YlOrRd" is named
## "brewer.yl_or_rd"
## Multiple palettes called "yl_or_rd" found: "brewer.yl_or_rd", "matplotlib.yl_or_rd". The first one, "brewer.yl_or_rd", is returned.
tmap_save(nyc_covid_map, "nyc_covid_interactive.html")
## [cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "YlOrRd" is named
## "brewer.yl_or_rd"
## Multiple palettes called "yl_or_rd" found: "brewer.yl_or_rd", "matplotlib.yl_or_rd". The first one, "brewer.yl_or_rd", is returned.
##
## Interactive map saved to C:\Users\dwvil\Documents\SPRING 2025\R LANGUAGE\R-Spatial\R-Spatial\Section_08\nyc_covid_interactive.html