R Week 09 Assignment

Author

Victoria Campbell

Published

February 22, 2025

Explanation of the Site

This Rpub includes the code used for week 08’ lab.

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 # 9

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

task 1:

Plot at least two high-quality static maps with one using the COVID-19 data and one using a related factor. You can use either plot method for sf or ggplot method.

Show the code
nyc_covid <- readRDS("nyc_covid.rds")

plot(nyc_covid)

Show the code
covidPopZipNYC <- readRDS("covidPopZipNYC.rds")

plot(
  covidPopZipNYC["whitePop"],
  main = "White Covid Cases",
  breaks = "quantile",       
  border = NA,
  graticule = TRUE           
)

Show the code
plot(
  covidPopZipNYC["totPop"],
  main = "Total Covid Cases",
  breaks = "quantile",       
  border = NA,
  graticule = TRUE           
)

task 2:

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.

Show the code
breaks_qt <- classIntervals(c(min(covidPopZipNYC$asianPop) - .00001,
                              covidPopZipNYC$asianPop), n = 6, style = "quantile")
covidPopZipNYC <- mutate(covidPopZipNYC, 
                    covid_cat = cut(asianPop, breaks_qt$brks,
                                    labels = c("Extremely Low", "Very Low", "Low", "Moderate", "High", "Very High"),
                                    dig.lab = 4, digits=1)) 
#plot:
plot1<- ggplot(covidPopZipNYC) + 
  geom_sf(aes(fill=covid_cat))+
  scale_fill_brewer(palette = "OrRd", name='Covid Case Categories') +
  labs(x='Longitude', y='Latitude', 
       title='Asian Covid Case')+
  coord_sf(crs = st_crs(2263))


breaks_male <- classIntervals(c(min(covidPopZipNYC$malePctg) - .00001,
                                  covidPopZipNYC$malePctg), n = 2, style = "quantile")
Warning in classIntervals(c(min(covidPopZipNYC$malePctg) - 1e-05,
covidPopZipNYC$malePctg), : var has missing values, omitted in finding classes
Show the code
MC_Final <- mutate(covidPopZipNYC, 
                   pt_cat = cut(malePctg, breaks_male$brks,
                                   labels = c("Higher", "Lower"),  
                                   dig.lab = 2, digits=1))
plot2<- ggplot(MC_Final) + 
  geom_sf(aes(fill=pt_cat))+
  scale_fill_brewer(palette = "Greens", name="Percentage/Threshold") +
  labs(x='Longitude', y='Latitude', 
       title='Male Percentage')+
  coord_sf(crs = st_crs(2263))

ggarrange(plot1, plot2, nrow = 1, ncol = 2)

task 3:

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

Show the code
covid_map <- tm_shape(covidPopZipNYC) +
  tm_polygons("Positive", 
              palette = "Oranges", 
              title = "Positive COVID Cases",
              border.col = "blue", 
              style = "quantile",  # smart classification
              n = 5) +              # number of levels (optional)
  tm_layout(title = "NYC Positive COVID Cases by ZIP Code")
── 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()`: migrate the argument(s) related to the legend of the
visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'
[v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`
Show the code
covid_map
[cols4all] color palettes: use palettes from the R package cols4all. Run
`cols4all::c4a_gui()` to explore them. The old palette name "Oranges" is named
"brewer.oranges"
Multiple palettes called "oranges" found: "brewer.oranges", "matplotlib.oranges". The first one, "brewer.oranges", is returned.

Show the code
# Save it as an HTML file
tmap_save(covid_map, "covid_map.html")
[cols4all] color palettes: use palettes from the R package cols4all. Run
`cols4all::c4a_gui()` to explore them. The old palette name "Oranges" is named
"brewer.oranges"
Multiple palettes called "oranges" found: "brewer.oranges", "matplotlib.oranges". The first one, "brewer.oranges", is returned.

Interactive map saved to D:\R-Spatial\covid_map.html