DEP_Data <- read_rds("../3_Intermediate/DEP_Data.rds")
head(DEP_Data)
##   unique_key        created_date         closed_date agency
## 1   23013899 2012-04-06 04:25:00 2012-04-16 04:25:00    DEP
## 2   25627963 2013-05-29 05:54:00 2013-05-29 12:21:00    DEP
## 3   25627964 2013-05-29 07:34:00 2013-05-31 12:30:00    DEP
## 4   23014578 2012-04-06 05:11:00 2012-04-16 05:11:00    DEP
## 5   37475111 2017-10-19 09:18:00 2017-10-24 08:51:00    DEP
## 6   23015156 2012-04-06 09:54:00 2012-04-16 09:54:00    DEP
##                              agency_name complaint_type
## 1 Department of Environmental Protection          Noise
## 2 Department of Environmental Protection          Noise
## 3 Department of Environmental Protection          Noise
## 4 Department of Environmental Protection          Noise
## 5 Department of Environmental Protection   Water System
## 6 Department of Environmental Protection          Noise
##                            descriptor incident_zip             incident_address
## 1  Noise: Private Carting Noise (NQ1)        11411 117-01 SPRINGFIELD BOULEVARD
## 2            Noise, Barking Dog (NR5)        11233            744 HALSEY STREET
## 3 Noise: Construction Equipment (NC1)        10013            7 HARRISON STREET
## 4  Noise: Private Carting Noise (NQ1)        10038                 DUTCH STREET
## 5           Leak (Use Comments) (WA2)        11357             14-25 139 STREET
## 6  Noise: Private Carting Noise (NQ1)        11375                         <NA>
##             street_name cross_street_1 cross_street_2 address_type
## 1 SPRINGFIELD BOULEVARD    LINDEN BLVD         117 RD      ADDRESS
## 2         HALSEY STREET    PATCHEN AVE      RALPH AVE      ADDRESS
## 3       HARRISON STREET      STAPLE ST   GREENWICH ST      ADDRESS
## 4          DUTCH STREET    JOHN STREET  FULTON STREET    BLOCKFACE
## 5            139 STREET         14 AVE         15 AVE      ADDRESS
## 6                  <NA>           <NA>           <NA> INTERSECTION
##              city facility_type status
## 1 Cambria Heights           N/A Closed
## 2        BROOKLYN           N/A Closed
## 3        NEW YORK           N/A Closed
## 4        NEW YORK           N/A Closed
## 5      Whitestone           N/A Closed
## 6    Forest Hills           N/A Closed
##                                                                                                                                                                                                                                                                                                                   resolution_description
## 1                                                                                                                                          The status of this Service Request is currently not available online. Please call 311 for further assistance. If you are outside of New York City, please call (212) NEW-YORK (212-639-9675).
## 2                                                                                                                                           The Department of Environmental Protection determined that an inspection is not warranted to investigate this complaint at this time and sent a letter to the complainant and/or respondent.
## 3 The Department of Environmental Protection did not observe a violation of the New York City Air/Noise Code at the time of inspection and could not issue a notice of violation. If the problem still exists, please call 311 and file a new complaint. If you are outside of New York City, please call (212) NEW-YORK (212-639-9675).
## 4                                                                                                                                          The status of this Service Request is currently not available online. Please call 311 for further assistance. If you are outside of New York City, please call (212) NEW-YORK (212-639-9675).
## 5                                                                                                                                                                   The Department of Environmental Protection determined that this complaint is a duplicate of a previously filed complaint. The original complaint is being addressed.
## 6                                                                                                                                          The status of this Service Request is currently not available online. Please call 311 for further assistance. If you are outside of New York City, please call (212) NEW-YORK (212-639-9675).
##   resolution_action_updated_date community_board        bbl   borough
## 1            2012-04-16 04:25:00       13 QUEENS 4127340008    QUEENS
## 2            2013-05-29 12:21:00     03 BROOKLYN 3016680033  BROOKLYN
## 3            2013-05-31 12:30:00    01 MANHATTAN 1001807512 MANHATTAN
## 4            2012-04-16 05:11:00    01 MANHATTAN       <NA> MANHATTAN
## 5            2017-10-24 08:51:00       07 QUEENS 4041070057    QUEENS
## 6            2012-04-16 09:54:00       06 QUEENS       <NA>    QUEENS
##   x_coordinate_state_plane y_coordinate_state_plane open_data_channel_type
## 1                  1055343                   192986                UNKNOWN
## 2                  1005228                   188750                  PHONE
## 3                   981619                   201118                  PHONE
## 4                   982189                   197759                UNKNOWN
## 5                  1031371                   225613                  PHONE
## 6                  1027799                   201983                UNKNOWN
##   park_facility_name park_borough           latitude          longitude
## 1        Unspecified       QUEENS  40.69609320344547 -73.74362116515917
## 2        Unspecified     BROOKLYN 40.684727057881254 -73.92436116842451
## 3        Unspecified    MANHATTAN 40.718698788216436 -74.00949123734148
## 4        Unspecified    MANHATTAN 40.709479286258514 -74.00743395461726
## 5        Unspecified       QUEENS  40.78580613055689 -73.82984126011773
## 6        Unspecified       QUEENS  40.72096620537131 -73.84289312884077
##    location.latitude location.longitude
## 1  40.69609320344547 -73.74362116515917
## 2 40.684727057881254 -73.92436116842451
## 3 40.718698788216436 -74.00949123734148
## 4 40.709479286258514 -74.00743395461726
## 5  40.78580613055689 -73.82984126011773
## 6  40.72096620537131 -73.84289312884077
##                                location.human_address intersection_street_1
## 1 {"address": "", "city": "", "state": "", "zip": ""}                  <NA>
## 2 {"address": "", "city": "", "state": "", "zip": ""}                  <NA>
## 3 {"address": "", "city": "", "state": "", "zip": ""}                  <NA>
## 4 {"address": "", "city": "", "state": "", "zip": ""}                  <NA>
## 5 {"address": "", "city": "", "state": "", "zip": ""}                  <NA>
## 6 {"address": "", "city": "", "state": "", "zip": ""}      QUEENS BOULEVARD
##   intersection_street_2 due_date location_type
## 1                  <NA>     <NA>          <NA>
## 2                  <NA>     <NA>          <NA>
## 3                  <NA>     <NA>          <NA>
## 4                  <NA>     <NA>          <NA>
## 5                  <NA>     <NA>          <NA>
## 6               71 ROAD     <NA>          <NA>
IDA_Data <-  DEP_Data %>% 
    filter(created_date >= as.Date("2021-08-26")) %>% 
    filter(created_date <= as.Date("2021-09-04")) 

IDA_Date <- IDA_Data%>% 
    mutate(day = day(created_date),
           month = month(created_date),
           year = year(created_date)) %>% 
    group_by(complaint_type,year,month,day) %>% 
    count() %>% 
    mutate(date = as.Date(paste0(year,"-",month,"-",day)))

names(IDA_Date)
## [1] "complaint_type" "year"           "month"          "day"           
## [5] "n"              "date"
ggplot(IDA_Date, aes(IDA_Date$date, n ,color = IDA_Date$complaint_type)) +
    geom_line() +
    scale_color_viridis(discrete = T) +
    theme_minimal() +
    xlab("Ida Timeframe") +
    scale_y_continuous(label=comma)

ggsave("../4_Outputs/IDA_complaints.png")
## Saving 7 x 5 in image
Sandy_Data <-  DEP_Data %>% 
    filter(created_date >= as.Date("2012-10-22")) %>% 
    filter(created_date <= as.Date("2012-11-2")) 

Sandy_Date <- Sandy_Data %>% 
    mutate(day = day(created_date),
           month = month(created_date),
           year = year(created_date)) %>% 
    group_by(complaint_type,year,month,day) %>% 
    count() %>% 
    mutate(date = as.Date(paste0(year,"-",month,"-",day)))

ggplot(Sandy_Date, aes(Sandy_Date$date, n ,color = Sandy_Date$complaint_type)) +
    geom_line() +
    scale_color_viridis(discrete = T) +
    theme_minimal() +
    xlab("Sandy Timeframe") +
    scale_y_continuous(label=comma)

ggsave("../4_Outputs/Sandy_complaints.png")
## Saving 7 x 5 in image
DEP_points <- read_rds("../3_Intermediate/DEP_points.rds")

IDA_Data <-  DEP_points %>% 
    filter(created_date >= as.Date("2021-08-26")) %>% 
    filter(created_date <= as.Date("2021-09-04"),
           complaint_type == "Sewer") %>%
    mutate(day = day(created_date),
           month = month(created_date),
           year = year(created_date),
           date = as.Date(paste0(year,"-",month,"-",day)))

Sandy_Data <- DEP_points %>% 
    filter(created_date >= as.Date("2012-10-22")) %>% 
    filter(created_date <= as.Date("2012-11-2"),
           complaint_type == "Sewer") %>% 
    mutate(day = day(created_date),
           month = month(created_date),
           year = year(created_date),
           date = as.Date(paste0(year,"-",month,"-",day)))
boroughs <- read_sf("../2_Data/borough_boundaries/geo_export_b5bd9a81-0a45-411a-a23f-8a1983296a03.shp")
tracts <- read_sf("../2_Data/ct_2010/geo_export_1c19ce5f-d77c-4a3f-adbe-7456e4a782a6.shp")
parks <- read_sf("../2_Data/Open Space (Parks)/geo_export_2addf878-38fa-4c00-8396-5aa5bf54644c.shp")
anim_ida <- tm_shape(tracts) +
    tm_polygons(lwd = .1) +
    tm_shape(parks) +
    tm_fill(col = "#99d8c9",alpha = .5)+
    tm_shape(IDA_Data)+
    tm_dots() +
    tm_facets(along = "date", free.coords = FALSE) +
    tm_layout(
              main.title.size = 1,
              main.title.fontface = 2
    )

anim_ida

## ==================

## =========

## =========

## ========

## =========

## =========

## =========

## =========

tmap_animation(tm=anim_ida, filename = "../4_Outputs/Ida.gif", delay = 100)
## Creating frames
## ==================
## =========
## =========
## ========
## =========
## =========
## =========
## =========
## 
## Creating animation
## Animation saved to /Users/seanchew/Desktop/Bo_Assistantship/4_Outputs/Ida.gif
anim_sandy <- tm_shape(tracts) +
    tm_polygons(lwd = .1) +
    tm_shape(parks) +
    tm_fill(col = "#99d8c9",alpha = .5)+
    tm_shape(Sandy_Data)+
    tm_dots() +
    tm_facets(along = "date", free.coords = FALSE) +
    tm_layout(
              main.title.size = 1,
              main.title.fontface = 2
    )

anim_sandy

## ===============

## =======

## =======

## =======

## ========

## =======

## =======

## =======

## ========

## =======

tmap_animation(tm=anim_sandy, filename = "../4_Outputs/sandy.gif", delay = 100)
## Creating frames
## ===============
## =======
## =======
## =======
## ========
## =======
## =======
## =======
## ========
## =======
## 
## Creating animation
## Animation saved to /Users/seanchew/Desktop/Bo_Assistantship/4_Outputs/sandy.gif