Show the code
require(tidyverse)
require(sf)
b<-8/4
print(b)[1] 2
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.
You can name the code chunk and also set options.
require(tidyverse)
require(sf)
b<-8/4
print(b)[1] 2
smpCode <- "hello, R markdown and RPubs!"
cat(smpCode)hello, R markdown and RPubs!
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"
unzip("R-Spatial_I_Lab.zip", exdir = "data_RS_WEEK_7")
list.of.packages <- c("sf", "sp", "spatial", "maptools", "rgeos","rgdal",
"raster", "grid", "rasterVis",
"tidyverse", "magrittr", "ggpubr", "lubridate",
"devtools", "htmlwidgets", "mapview",
"classInt", "RColorBrewer", "ggmap", "tmap", "leaflet", "mapview",
"ggrepel", "ggsn",
"spdep","spatialreg","GWmodel");# Unzip the inner zip file
unzip("data_RS_WEEK_7/ZIP_CODE_040114.zip", exdir = "data_RS_WEEK_7/NewZIP")nyc_sf_ZIP <- st_read("data_RS_WEEK_7/NewZIP/ZIP_CODE_040114.shp")Reading layer `ZIP_CODE_040114' from data source
`C:\Users\Alijah Anyagwosi\Downloads\SPRING 2025\RStudio\data_RS_WEEK_7\NewZIP\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)
str(nyc_sf_ZIP)Classes 'sf' and 'data.frame': 263 obs. of 13 variables:
$ ZIPCODE : chr "11436" "11213" "11212" "11225" ...
$ BLDGZIP : chr "0" "0" "0" "0" ...
$ PO_NAME : chr "Jamaica" "Brooklyn" "Brooklyn" "Brooklyn" ...
$ POPULATION: num 18681 62426 83866 56527 72280 ...
$ AREA : num 22699295 29631004 41972104 23698630 36868799 ...
$ STATE : chr "NY" "NY" "NY" "NY" ...
$ COUNTY : chr "Queens" "Kings" "Kings" "Kings" ...
$ ST_FIPS : chr "36" "36" "36" "36" ...
$ CTY_FIPS : chr "081" "047" "047" "047" ...
$ URL : chr "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" ...
$ SHAPE_AREA: num 0 0 0 0 0 0 0 0 0 0 ...
$ SHAPE_LEN : num 0 0 0 0 0 0 0 0 0 0 ...
$ geometry :sfc_POLYGON of length 263; first list element: List of 1
..$ : num [1:159, 1:2] 1038098 1038142 1038171 1038280 1038521 ...
..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
..- attr(*, "names")= chr [1:12] "ZIPCODE" "BLDGZIP" "PO_NAME" "POPULATION" ...
plot(st_geometry(nyc_sf_ZIP), main='Pure geometry with st_geometry function')
# Load NYS Health Facility Data
health_data <- read.csv("data_RS_WEEK_7/NYS_Health_Facility.csv")
#rfs_xy_data <- read.csv("data_RS_WEEK_7/nys_retail_food_store_xy.csv")
rfs_data <- read.csv("data_RS_WEEK_7/NYS_Retail_Food_Stores.csv")plot(nyc_sf_ZIP['PO_NAME'], main='One column with ["PO_NAME"]')
# Two columns, with geometry
plot(nyc_sf_ZIP[c('PO_NAME', 'POPULATION')], main='Two Columns with ["PO_NAME", "POPULATION"]')
#
nyc_sf_HighPOP <- nyc_sf_ZIP[nyc_sf_ZIP$POPULATION > 100000, ]
plot(st_geometry(nyc_sf_ZIP))
plot(st_geometry(nyc_sf_HighPOP), add=T, col="blue")
#
nyc_sf_LowPOP <- nyc_sf_ZIP[nyc_sf_ZIP$POPULATION < 5000, ]
plot(st_geometry(nyc_sf_ZIP))
plot(st_geometry(nyc_sf_LowPOP), add=T, col="red")
plot(nyc_sf_ZIP %>% st_geometry(), col = 'NA', bg= "black", border='white', lwd=0.5)
nyc_sf_ZIP %>%
filter(POPULATION > 100000) %>%
st_geometry() %>%
plot(col="yellow", add=T)
# We use all the default options, but select a few columns.
# If we use all columns, the popup window will be too big.
nyc_sf_ZIP %>% dplyr::select(PO_NAME, ZIPCODE, POPULATION) %>%
mapview::mapview()nyc_sf_ZIP <- st_transform(nyc_sf_ZIP, crs = 4326)nyc_sf_ZIP %>% dplyr::select(PO_NAME, ZIPCODE, POPULATION, AREA) -> tmp_sf_ZIP
mapview(tmp_sf_ZIP, zcol='PO_NAME', layer.name = 'Neighborhood or Borough', stroke=FALSE) +
mapview(nyc_sf_ZIP, zcol='POPULATION',
layer.name = 'Total Population',
homebutton=FALSE,
legend=FALSE,
hide = TRUE,
label="Population",
popup = 'Population') +
mapview(nyc_sf_ZIP, zcol="AREA",
layer.name = 'Area of the Zipcode',
homebutton=FALSE,
legend=FALSE,
hide=TRUE,
label="Land Area",
popup = 'Land Area')