knitr::opts_chunk$set(fig.align = 'center', echo = TRUE, warning = FALSE, message = FALSE)
## Loading libraries

# cleaning
library(tidyverse)
library(lubridate)

# spatial
library(sp)
library(raster)
library(rgdal)
library(leaflet)
library(wesanderson)

Background:

Housing and access to housing in San Francisco are features closely interconnected with the health of San Francisco’s resident population. This map seeks to observe No-Fault Evictions filed in the city and county of San Francisco while COVID-19 Shelter in Place restrictions were active.

Source: San Francisco Eviction Notice Data

# read data from local source. 
en_data <- read_csv("Eviction_Notices.csv")

# format column names.
names(en_data) %<>% 
  stringr::str_replace_all("\\s","_") %>% tolower

# format dates.
en_data$file_date <- mdy(en_data$file_date)
## cleaning
map_data <- en_data %>% 
  # select columns of interest.
  dplyr::select("file_date",
                "non_payment",
                "breach",
                "nuisance",
                "illegal_use",
                "failure_to_sign_renewal",
                "access_denial",
                "late_payments",
                "location") %>%                             
  # filter logic for no-fault evicition criteria. 
  filter(non_payment == "FALSE" &
        breach == "FALSE" &
        nuisance == "FALSE" &
        illegal_use == "FALSE" &
        failure_to_sign_renewal == "FALSE" &
        access_denial == "FALSE" &
        late_payments == "FALSE") %>% 
  
  # filter date post CA-Shelter in Place dates.
  filter(file_date >= "2020-03-01") %>% 
  # purge NA in location vector.
  filter(location != "NA") %>% 
  
  # catergorise dates for legend, viewing simplicity. 
  mutate(file_date = 
           case_when(
    file_date < "2020-06-01" ~ "Spring 2020",
      file_date >= "2020-06-01" & 
      file_date < "2020-10-01"  ~ "Summer 2020", 
      file_date >= "2020-10-01" & 
      file_date < "2020-12-01"  ~ "Fall 2020",
      file_date >= "2020-12-01" &
      file_date < "2021-03-01" ~ "Winter 2020-2021")) %>% 
  mutate(file_date = as.factor(file_date)) %>% 
  
  # clean location string.
  mutate(location = as.character(location)) %>% 
  mutate(location = str_remove_all(location, "\\Q(\\E")) %>%
  mutate(location = str_remove_all(location, "\\Q)\\E")) %>%
  
  # split location string.
  separate(location, c("lat", "long"), sep = ",") %>% 
  # structure as numeric 
  mutate(lat = as.numeric(lat)) %>% 
  mutate(long = as.numeric(long)) %>%
 
   # select columns for mapping. 
  dplyr::select(file_date, lat, long)
# format sp data object
sip_evict_data_SPDF <- SpatialPointsDataFrame(
  coords = map_data[, c("long","lat")],
  data = map_data[, c("file_date")],
  proj4string = CRS("+init=epsg:4326"))

# import shapefile from local source. 
sf_areas <- readOGR("sf_nhoods", "geo_export_30b7fc05-1dbd-4c36-829f-86d3ecdd2165")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/avery/Desktop/PH272C/sf_nhoods", layer: "geo_export_30b7fc05-1dbd-4c36-829f-86d3ecdd2165"
## with 117 features
## It has 2 fields
# define palette
pal <- 
   colorFactor(palette = wes_palette("Cavalcanti1")[1:5], 
               levels = c("Spring 2020",
                          "Summer 2020", 
                          "Fall 2020",
                          "Winter 2020-2021"
                          ))
# create map
basemap <- leaflet() %>% addProviderTiles("CartoDB.Positron")
basemap %>% addPolygons(data=sf_areas, color = "black", 
                        weight = .5, fillOpacity = .01, 
                        popup = sf_areas$name) %>% 
  addCircleMarkers(data = sip_evict_data_SPDF, 
              color = pal(sip_evict_data_SPDF$file_date),
                   radius = .5,
                   popup = paste("<p>", "Date Filed:",
                    (sip_evict_data_SPDF$file_date),
                                 "<p>")) %>% 
  addLegend(pal = pal,
    title = "No Fault Evictions Filed During SIP",
            values = sip_evict_data_SPDF$file_date,
            position = "bottomleft")

~fin