This document contains the code for the dashboard of NYC Restaurant inspection data from 2012 to 2017. We begin by loading the following packages
library(flexdashboard)
library(p8105.datasets)
library(plotly)
library(tidyverse)
We load the data with the p1805
data sets package. We
clean the date variable and create a variable for the restaurant being
closed as a result of the inspection.
rest_inspec =
p8105.datasets::rest_inspec |>
mutate(
year_month = str_sub(inspection_date, 0, 7),
year = year(inspection_date),
month = year(inspection_date),
closed_flag = action %in% c(
"Establishment Closed by DOHMH. Violations were cited in the following area(s) and those requiring immediate action were addressed.",
"Establishment re-closed by DOHMH")
)
rest_inspec |>
drop_na(grade, score) |>
filter(grade %in% c('A', 'B', 'C')) |>
group_by(year_month) |>
count(grade) |>
plot_ly(x = ~year_month, y = ~n, color = ~grade, type = 'scatter', mode = 'line')|>
layout(xaxis = list(title = 'Time'),
yaxis = list(title = 'Number of Grades Issued'))
rest_inspec |>
drop_na(score) |>
filter(grade %in% c('A', 'B', 'C')) |>
group_by(grade)|>
plot_ly(x = ~grade, color = ~grade, y = ~score, type = 'box' )|>
layout(xaxis = list(title = 'Grade'),
yaxis = list(title = 'Score'))
rest_inspec |>
filter(boro != 'Missing') |>
group_by(boro) |>
summarize(perc_closed = mean(closed_flag)) |>
plot_ly(x = ~boro, y = ~perc_closed, color = ~boro, type = "bar") |>
layout(xaxis = list(title = 'Borough'),
yaxis = list(title = 'Proportion'))