knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
                      warning=FALSE, message=FALSE)
library(tidyverse)
library(httr)
library(jsonlite)
library(plotly)
library(leaflet)
library(knitr)

theme_set(
  ggthemes::theme_fivethirtyeight() + theme(legend.position = "bottom")
  )

options(
  ggplot2.continuous.colour = "viridis",
  ggplot2.continuous.colour = "viridis"
)

scale_colour_discrete = scale_color_viridis_d
scale_fill_discrete = scale_fill_viridis_d

api_url = "https://data.cityofnewyork.us/resource/43nn-pn8j.csv"

rest_inspect = 
  GET(api_url, query = list("$limit" = 50000)) %>% 
  content("parsed")

rest_inspect = rest_inspect %>% 
  filter(grade %in% c("A", "B", "C"), boro != "0")

italian = c("Italian", "Italian/Pizza")
asian = c("Asian", "Japanese", "Korean", "Chinese")

Sys.setenv('MAPBOX_TOKEN' = 'pk.eyJ1IjoiYWFicmFtb3Y5MCIsImEiOiJja2gyZm5obzQwNWIxMnFxc3phcWh1MWtwIn0.amAvJHtFkTl9XWJ68fh96Q')

New Restaurants in New York by Borough

rest_inspect %>% 
  filter(str_detect(inspection_type, "Initial")) %>% 
  separate(grade_date, sep="-", into = c("grade_year", "grade_month", "grade_day")) %>%
  count(boro, grade_year) %>% 
  plot_ly(x = ~grade_year, y = ~n, color = ~boro, type = "bar", colors = "viridis") %>%
    layout(title = "New Restaurants in NYC",
         xaxis = list(title = "Year", zeroline = TRUE),
         yaxis = list(title = "Restaurants (n)", zeroline = TRUE))

Asian Restaurants

rest_inspect %>% 
  filter(str_detect(cuisine_description, asian)) %>% 
  filter(latitude > 0) %>% 
  mutate(
    text_label = str_c("Name: ", dba, "\nGrade: ", grade)) %>% 
  plot_mapbox(
    x = ~longitude, y = ~latitude, type = "scattermapbox", color =~grade, text =~text_label) %>% 
  layout(
    mapbox = list(
      zoom = 10,
      center = list(lat = 40.67, lon = -73.97)))

Italian and Pizza Restaurants

rest_inspect %>% 
  filter(str_detect(cuisine_description, italian)) %>% 
  filter(latitude > 0) %>% 
  mutate(
    text_label = str_c("Name: ", dba, "\nGrade: ", grade)) %>% 
  plot_mapbox(x = ~longitude, y = ~latitude, color =~grade, text =~text_label) %>% 
  layout(
    mapbox = list(
      zoom = 10,
      center = list(lat = 40.67, lon = -73.97)))

Mexican

rest_inspect %>% 
  filter(str_detect(cuisine_description, "Mexican")) %>% 
  filter(latitude > 0) %>% 
  mutate(
    text_label = str_c("Name: ", dba, "\nGrade: ", grade)) %>% 
  plot_mapbox(x = ~longitude, y = ~latitude, color =~grade, text =~text_label) %>% 
  layout(
    mapbox = list(
      zoom = 10,
      center = list(lat = 40.67, lon = -73.97)))

Boxplot of Restaurant Inspection Scores

rest_inspect %>% 
  group_by(boro) %>% 
  filter(score %in% 1:30)  %>% 
  plot_ly(
    y = ~score, x = ~boro, color = ~boro,
    type = "box", colors = "viridis") %>% 
  layout(title = "Distribution of Scores",
         xaxis = list(title = "Borough", zeroline = TRUE),
         yaxis = list(title = "Score", zeroline = TRUE))