load in dataframes and convert to sf
# read in zip codes as sf
nyc_zip_sf <- st_read("ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source
## `C:\Users\andyx\Downloads\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)
# read in health facilities filter out NA coord rows
#also filtered out rows where coords were 0 or latitude and longitude values were switched
health_1 <- read_csv(file.path(getwd(),"NYS_Health_Facility.csv"
),show_col_types = FALSE)
health_2 <- health_1 %>%
filter(!is.na(`Facility Longitude`) & !is.na(`Facility Latitude`)
& `Facility Longitude` != 0 & `Facility Latitude` != 0
& `Facility Latitude` >0)
health_sf <- st_as_sf(health_2,
coords = c("Facility Longitude", "Facility Latitude"),
crs=4326)
# read in food stores
food <- read_csv(file.path(getwd(), "nys_retail_food_store_xy.csv"
),show_col_types = FALSE)
food1 <- food %>%
filter(!is.na(X) & !is.na(Y)) %>%
filter(County %in% c("Bronx", "Kings", "New York", "Queens", "Richmond"))
food_stores_sf <- st_as_sf(food1,
coords = c("X", "Y"),
crs = 4326)
use mapview to view each sf with basemap
using mapview
mapview(nyc_zip_sf, zcol = "COUNTY", layer.name = "nyc zip codes")
using ggmap
#api key and basemap for ny state
register_stadiamaps(key="77550fe1-8725-4db8-b595-4408807c9c2a", write=FALSE)
health_sf %>% st_bbox() %>% as.vector() %>% get_stadiamap(zoom=10, messaging=FALSE)->basemap
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
## ℹ 396 tiles needed, this may take a while (try a smaller zoom?)
#add lon lat columns back to sf dataframe
health_coords <- st_coordinates(health_sf)
lon <- health_coords[, "X"]
lat <- health_coords[, "Y"]
health_sf$Longitude <- lon
health_sf$Latitude <- lat
#plot points colored on facility type
ggmap(basemap) +
geom_point(aes(x=Longitude, y=Latitude, color=Description),
data= health_sf,
size=1)
using mapview
food_stores_sf %>% mapview()