Reading layer `ZIP_CODE_040114' from data source
`/Users/ziruizheng/Desktop/College Class/Now/Data Analysis & Viz/2025_Spring_week 7-10/Section_07/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)
Rows: 620
Columns: 3
$ Facility.Name <chr> "Charles T Sitrin Health Care Center Inc", "East Side N…
$ Ownership.Type <chr> "Not for Profit Corporation", "Business Corporation", "…
$ geometry <POINT> POINT (-75.22883 43.05497), POINT (-78.12867 42.73898…
[1] "Facility.Name" "Ownership.Type" "geometry"
task 4: Read and process the NYS retail food stores
Rows: 287
Columns: 3
$ Entity.Name <chr> "ALL NATURAL CO INC ", "DUANE READE ETAL …
$ Square.Footage <dbl> 7250, 7300, 7500, 7500, 7500, 7500, 7500, 7500, 7500, 7…
$ geometry <POINT [°]> POINT (-73.9843 40.72898), POINT (-73.98229 40.76…
task 5.1: Create MapView() for the ZIP CODE sf
Notice:
Here is a full graph of zip code area above. I reduced the size of
the spatial file from 200 to 12, because mapview() renders the zip
code areas in polygon shapes instead of drawing dotted points, which
requires significant GPU by RPubs. Therefore the full graph cannot be
generated by RPubs, I attached a PNG version of this full graph
visualization for your reference :)
task 5.2: Create MapView() for the health facility sf
task 5.3: Create MapView() for the HEALTH FACILITY sf
Notice:
Here for the same reason of large file size issue, I have reduced the
size of retail_food spatial file by filtering out small
square.footage. Down below is a PNG version of this complete data
graph visualization for your reference :)
---title: "R Week 07 Assignment"author: "Zirui Zheng"date: "2/22/2025"format: html: toc: true toc-location: left code-fold: true code-summary: "Show the code" code-tools: true---## Explanation of the template```{r setup, include=FALSE}knitr::opts_chunk$set(echo = FALSE)```## R Spatial Lab Assignment \# 1### task 1: Import necessary libraries and data path```{r load_packages, include=FALSE}## Import librariesrequire(tidyverse);require(sf); require(mapview); require(magrittr);library(knitr);## Set the data pathdata_dir <- file.path(getwd(),"R-Spatial_I_Lab")options(viewer=NULL)```### task 2: Read the NYC postal areas in Shapefiles```{r}## Read in the Shapefiles of zip codezip_code_sf <-st_read(file.path(data_dir,'ZIP_CODE_040114/ZIP_CODE_040114.shp'))zip_code_sf <- zip_code_sf %>%select(ZIPCODE, POPULATION, geometry, SHAPE_AREA, SHAPE_LEN) %>%filter(POPULATION >100& POPULATION <4000)glimpse(zip_code_sf)```### task 3: Read and process the NYS health facilities```{r}## Read filehealth_facility_df <-read.csv(file.path(data_dir,'NYS_Health_Facility.csv'))colnames(health_facility_df)## Create sf health_facility_sf <- health_facility_df %>%filter(Short.Description =="NH"& Facility.State =="New York") %>%select(Facility.Name, Ownership.Type, Facility.Longitude, Facility.Latitude) %>%## remove the na values from the list, otherwise sf() cannot workfilter(!is.na(Facility.Longitude) , !is.na(Facility.Latitude)) %>%st_as_sf(coords =c("Facility.Longitude", "Facility.Latitude"))glimpse(health_facility_sf)colnames(health_facility_sf) ## Check geometry col is created## set EPSG code to 4326st_crs(health_facility_sf) <-4326```### task 4: Read and process the NYS retail food stores```{r}## Read file## The fileEncoding method is from https://stackoverflow.com/questions/14363085/invalid-multibyte-string-in-read-csvretail_food_df <-read.csv(file.path(data_dir,'nys_retail_food_store_xy.csv'), fileEncoding="latin1")colnames(retail_food_df)## Create sf # Get rid of ',' in "Square.Footage" col. ex. "27,000" -> "27000". The gsub method is from https://stackoverflow.com/questions/13590139/remove-numbers-from-alphanumeric-charactersretail_food_df$Square.Footage <-as.numeric(gsub(",", "", retail_food_df$Square.Footage))retail_food_sf <- retail_food_df %>%# filter license number to remove an edge pointfilter(str_starts(City, "NEW YORK") & Square.Footage >7000& License.Number !="305241") %>%select(Entity.Name, Square.Footage, X, Y) %>%## remove the na values from the list, otherwise sf() cannot workfilter(!is.na(X) , !is.na(Y)) %>%arrange(Square.Footage) %>%st_as_sf(coords =c("X", "Y"))## set EPSG code to 4326st_crs(retail_food_sf) <-4326glimpse(retail_food_sf)```### task 5.1: Create MapView() for the ZIP CODE sf```{r mapview_1}mapview(zip_code_sf, zcol= 'POPULATION', layer.name='Zip Code')long_text <- '\n\nNotice:\n\nHere is a full graph of zip code area above. I reduced the size of the spatial file from 200 to 12, because mapview() renders the zip code areas in polygon shapes instead of drawing dotted points, which requires significant GPU by RPubs. Therefore the full graph cannot be generated by RPubs, I attached a PNG version of this full graph visualization for your reference :)'wrapped_text <- strwrap(long_text, width = 70)cat(wrapped_text, sep = "\n")include_graphics(file.path(data_dir, "zip_code_ful.PNG"))```### task 5.2: Create MapView() for the health facility sf```{r mapview_2}mapview(health_facility_sf, zcol='Ownership.Type', layer.name='Health Facility')```### task 5.3: Create MapView() for the HEALTH FACILITY sf```{r mapview_3}mapview(retail_food_sf, zcol='Square.Footage', layer.name='Retail Food')long_text <- '\n\nNotice:\n\nHere for the same reason of large file size issue, I have reduced the size of retail_food spatial file by filtering out small square.footage. Down below is a PNG version of this complete data graph visualization for your reference :)'wrapped_text <- strwrap(long_text, width = 70)cat(wrapped_text, sep = "\n")include_graphics(file.path(data_dir, "Retail_food.PNG"))```### task 6: Save 3 objects in a RData```{r save}data_dirsave(zip_code_sf, health_facility_sf, retail_food_sf, file = file.path('./', "assignment_1.RData"))```