library(tidyverse)
library(p8105.datasets)
library(plotly)
library(leaflet)
library(tidygeocoder)
library(ggridges)

Filtering NYC restaurant data to include only restaurants in the Bronx, the most recent inspections by restaurant, cases with a flag of Critical or Not Critical, and a grade of A, B or C.

data("rest_inspec")

nycrest =
  rest_inspec %>%
  group_by(camis) %>%
  slice(which.max(as.Date(inspection_date, "%Y-%m-%d", tz = "America/New_York"))) %>%
  filter(boro == "BRONX") %>%
  drop_na(score) %>%
  drop_na(grade) %>%
  filter(critical_flag %in% c("Critical", "Not Critical")) %>%
  filter(grade %in% c("A", "B", "C"))

nycrest2 =
nycrest %>%
    mutate(address = str_c(building, " " , street, " New York, NY", " ", zipcode))

addresses = 
  nycrest2 %>%
  geocode(address = address, method = "census", verbose = TRUE)
## 
## Number of Unique Addresses: 1848
## Executing batch geocoding...
## Batch limit: 10,000
## Passing 1,848 addresses to the US Census batch geocoder
## Querying API URL: https://geocoding.geo.census.gov/geocoder/locations/addressbatch
## Passing the following parameters to the API:
## format : "json"
## benchmark : "Public_AR_Current"
## vintage : "Current_Current"
## Query completed in: 288 seconds
addresses2_df =
  addresses %>%
  drop_na(lat) %>%
  mutate(restaurant_name = dba)

Presenting the Results in a Leaflet Map

pal = colorFactor("viridis", NULL)

addresses2_df %>%
  leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(~long, ~lat, radius = 1, color = ~pal(grade), popup = ~restaurant_name) %>%
    addLegend("bottomright", pal = pal, values = ~grade,
    title = "Restaurant Grade",
    opacity = 1)

Filtering the dataset to include only restaurants in Manhatta and the most recent inspections by restaurant.

cuisinerating =
  rest_inspec %>%
  group_by(camis) %>%
  slice(which.max(as.Date(inspection_date, "%Y-%m-%d", tz = "America/New_York"))) %>%
  filter(boro == "MANHATTAN") %>%
  drop_na(score) %>%
  drop_na(grade) %>%
  filter(cuisine_description != "Not Listed/Not Applicable") %>%
  group_by(cuisine_description) %>%
  summarise(avg_score = round(mean(score),0))

Presenting a scatterplot displaying average inspection score by cuisine type in Manhattan

cuisineplot =
cuisinerating  %>%
plot_ly(
    x = ~cuisine_description, y = ~avg_score, type = "scatter", mode = "markers", size = 0.3,
                             color = ~avg_score, alpha = 0.9)  %>%
    layout(yaxis = list(title = list(text = "Average Score")), 
           xaxis = list(title = list(text = "Cuisine Description"), 
                        showticklabels = FALSE))

colorbar(cuisineplot, title = "Average Score") 

Filtering the dataset to include the most recent inspections by restaurant and only grades A, B and C.

gradeboro =
  rest_inspec %>%
  group_by(camis) %>%
  slice(which.max(as.Date(inspection_date, "%Y-%m-%d", tz = "America/New_York"))) %>%
  filter(grade %in% c("A","B","C")) %>%
  filter(boro != "Missing") %>%
  drop_na(score) %>%
  drop_na(grade) %>%
  group_by(boro) %>%
  count(grade, name = "count_grade")

Presenting a bar graph displaying the count of A, B and C inspection grades received by NYC borough

gradeboro %>% 
  plot_ly(x = ~grade, y = ~count_grade, color = ~boro, type = "bar", colors = "viridis",
          facet_col = "boro") %>%
  layout(title = list(text = "Count of Inspection Grades Received by Borough", 
                        xanchor = 'center', yanchor =  'top', size = 8),
           yaxis = list(title = list(text = "Count")), 
           xaxis = list(title = list(text = "Grade")))
## Warning: 'bar' objects don't have these attributes: 'facet_col'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'bar' objects don't have these attributes: 'facet_col'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'bar' objects don't have these attributes: 'facet_col'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'bar' objects don't have these attributes: 'facet_col'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'bar' objects don't have these attributes: 'facet_col'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Filtering the dataset to include only Au Bon Pain locations in Manhattan.

time =
  rest_inspec %>%
  filter(grade %in% c("A","B","C")) %>%
  filter(boro == "MANHATTAN") %>%
  drop_na(score) %>%
  drop_na(grade) %>%
  filter(dba %in% c("AU BON PAIN")) %>%
  arrange(inspection_date) %>%
  mutate(name = str_c(building, " ", street)) 

Presenting a line graph displaying inspection scores over time by Au Bon Pain location in Manhattan

time  %>%
 plot_ly(
    x = ~inspection_date, y = ~score,
    type = "scatter",color = ~name, mode = "lines+markers", colors = "viridis") %>%
     layout(yaxis = list(title = list(text = "Score Received")), 
           xaxis = list(title = list(text = "Inspection Date")))