R Week 07 Assignment
#Lists packages, there was a comma missing from source code, I added it here.
list.of.packages <- c(“sf”, “sp”, “spatial”, “maptools”, “rgeos”,“rgdal”, “terra”, “grid”, “rasterVis”, “tidyverse”, “magrittr”, “ggpubr”, “lubridate”,“patchwork”, “devtools”, “htmlwidgets”, “mapview”, “classInt”, “RColorBrewer”, “ggmap”, “OpenStreetMap”, “tmap”, “leaflet”, “mapview”, “ggrepel”, “ggsn”, “spdep”,“spatialreg”,“GWmodel”);
Check out the packages that have not been installed yet.
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,“Package”])]
Install those missing packages first. It could take a long time for the first time.
if(length(new.packages)>0) install.packages(new.packages)
Load all packages.
lapply(list.of.packages,function(x) { require(x,character.only = TRUE,quietly = TRUE) })
Or load specific packages when needed
require(sp); require(sf); library(terra)
read in a Shapefile. With the ‘shp’ file extension, sf knows it is a Shapefile.
nyc_sf <- st_read(“data/nyc/nyc_acs_tracts.shp”)
looking at variable
str(nyc_sf)
#tuning Health Facilities into spatial object
#read csv health_df <- read_csv(“data/NYS_Health_Facility.csv”, show_col_types = FALSE, lazy = FALSE) #rename latitude and longitude health_df <- health_df %>% rename(“lat”= Facility Latitude, “long” = Facility Longitude)
#filter for specific lat/long values health_df <- health_df %>% filter(!is.na(lat),lat >= 40, lat <= 46, !is.na(long), long >= -80, long <= -72)
#see df str(health_df)
#make into spatial object health_sf <- st_as_sf(health_df, coords = c(“long”, “lat”)) str(health_df)
st_crs(health_sf) st_crs(health_sf) <- 4326 # EPSG st_crs(health_sf)
#filtering for specific zipcode and city to plot data <-health_sf %>% dplyr::filter(!is.na(Operator Zip Code), !is.na(health_sf\(`Facility City`), health_sf\)Facility City == “Albany”)
#plot ggplot(data) + geom_sf(aes(color=Operator Zip Code)) + coord_sf(xlim = c(-74.06, -73.85), default_crs = sf::st_crs(4326))
#same for brooklyn data <-health_sf %>% dplyr::filter(!is.na(Operator Zip Code), !is.na(health_sf\(`Facility City`), health_sf\)Facility City == “Brooklyn”)
ggplot(data) + geom_sf(aes(color=Facility County Code)) + coord_sf(xlim = c(-74.06, -73.85), default_crs = sf::st_crs(4326))
#NYS Retail Food Stores
retail_df <- read_csv(“data/nys_retail_food_store_xy.csv”, show_col_types = FALSE, lazy = FALSE) colnames(retail_df) <- c(‘county’, ‘Licnum’, ‘optype’, ‘etype’, ‘ename’, ‘dbname’, ‘snum’, ‘sname’, ‘alinetwo’, ‘alinethree’, ‘city’, ‘state’, ‘zip’, ‘sqft’, ‘location’, ‘coords’, ‘lat’, ‘long’)
colnames(retail_df)
#select wanted values, remove na values, and filter for specific lat/long values retail_df<- retail_df %>% select(county, etype,dbname, city, zip,state,lat, long) %>% filter(!is.na(county), !is.na(etype), !is.na(city),!is.na(zip),!is.na(state), !is.na(lat),lat >= 40, lat <= 46, !is.na(long), long >= -80, long <= -72)
#see df str(retail_df)
#make into spatial object retail_sf <- st_as_sf(retail_df, coords = c(“long”, “lat”))
st_crs(retail_sf) st_crs(retail_sf) <- 4326 # EPSG st_crs(retail_sf)
#filtering for nyc counties city_data <-retail_sf %>% dplyr::filter(county %in% c(“Bronx”,“Kings”, “New York”, “Richmond”, “Queens”))
#plot ggplot(city_data) + geom_sf(aes(color= etype)) + coord_sf(xlim = c(-73.06, -74.7), default_crs = sf::st_crs(4326))
mapview(city_data, zcol=‘etype’, layer.name=‘Establishment Type’)
mapview(data, zcol=‘Operator Zip Code’, layer.name=‘Operator Zip Code’)