Explanation of the template

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.

R Spatial Lab Assignment # 2

Don’t use a single chunk for the entire assignment. Break it into multiple chunks.

task 1:

smpCode <- "hello, R markdown and RPubs!"

cat(smpCode)
## hello, R markdown and RPubs!

task 2:

Quarto markdown is different from R markdown in terms of chunk options. See chunk options at Quarto website.

print("This is the new code chunk options available in Quarto Markdown")
## [1] "This is the new code chunk options available in Quarto Markdown"

task 4: Use ggplot2 and other ggplot-compatible packages to create a multi-map figure illustrating the possible relationship between COVID-19 confirmed cases or rate and another factor (e.g., the number of nursing homes, number of food stores, neighborhood racial composition, elderly population, etc.). The maps should be put side by side on one single page. Add graticule to at least one of those maps and label some of the feature on the map where applicable and appropriate.

# Prepare data to group maps together

plot1 <- ggplot(ACSCovidData) +
  geom_sf(aes(fill = POPULAT)) +
  scale_fill_viridis_c(
    name = "Population",
    labels = comma
  ) +
  labs(title = "NYC Population (per square km)")

# Label locations on map with over 1500 cases

label_data2 <- ACSCovidData %>%
  sf::st_point_on_surface() %>%                    
  filter(Positiv > 1500) %>%                       
  mutate(
    coords = sf::st_coordinates(.),                  
    x = coords[,1],
    y = coords[,2],
    POP_COMMA = comma(Positiv)                       
  )
## Warning: st_point_on_surface assumes attributes are constant over geometries
# Plot with labels using geom_label_repel
  
plot2 <- ggplot(ACSCovidData) +
  geom_sf(aes(fill = Positiv)) +
  scale_fill_viridis_c(
    name = "Cases",
    labels = comma
  ) +
  geom_label_repel(
    data = label_data2,
    aes(x = x, y = y, label = POP_COMMA),
    label.size = 0.1,
    size = 3,
    segment.color = "grey50",
    segment.size = 0.6,
    box.padding = 0.5,
    force = 5,
    max.overlaps = 100   # ← this line prevents that warning!
  ) +
  labs(
    title = "NYC Cases (per square km)",
    x = "Longitude",
    y = "Latitude"
  ) +
  coord_sf()

# Arrange maps together, ensure they are the same size

ggarrange(plot1, plot2, nrow = 1, ncol = 2, align ="h")

task 5: Create a web-based interactive map for COIVD-19 data using tmap, mapview, or leaflet package and save it as a HTML file.

# Create interactive map

interactive_map <- mapview(ACSCovidData, zcol = "Positiv")

# Convert to leaflet

leaflet_map <- interactive_map@map

# Save as html

saveWidget(leaflet_map, "COVID_map.html")