Column

Number of violations per restaurant by borough

Column

Restuarants with most violations in 2017 in 10001

---
title: "Dashboard: NYC Restaurant Inspections"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source: embed
    theme: sandstone
---

```{r setup, include=FALSE}
library(tidyverse)
library(p8105.datasets)
library(plotly)
library(lubridate)

data("rest_inspec")
```

Column {data-width=600}
-----------------------------------------------------------------------

### Number of violations per restaurant by borough

```{r}
rest_inspec %>% 
  filter(inspection_date > as.Date("2000-01-01"),
         boro != "Missing") %>% 
  group_by(camis) %>% 
  mutate(n_per_restaurant = n(),
         year = year(inspection_date)) %>% 
  filter(year > 2013) %>% 
  plot_ly(x = ~boro, y = ~n_per_restaurant, 
          color = ~boro, colors = "viridis", type = "box",
          frame = ~year) %>% 
   layout(xaxis = list(title = "Borough"),
         yaxis = list(title = "Violations per restaurant"),
         showlegend = FALSE)
```

Column {data-width=400}
-----------------------------------------------------------------------

### Food temperature related violations as a percentage of all violations over time

```{r}
rest_inspec %>% 
  mutate(year = year(inspection_date),
         month = month(inspection_date),
         year_month = make_date(year, month)) %>% 
  filter(inspection_date > as.Date("2013-12-01")) %>% 
  group_by(year_month) %>% 
  mutate(total_monthly_viol = n()) %>% 
  filter(violation_code == "02G" | violation_code == "02B") %>% 
  group_by(year_month, violation_code) %>% 
  summarize(viol_per_type = n(),
            total_monthly_viol = mean(total_monthly_viol)) %>% # keep total violations in the month and violations per violation code
  mutate(percent_viol = (viol_per_type/total_monthly_viol)*100) %>% 
  ungroup() %>% 
  mutate(violation_code = recode(violation_code, "02G" = "Cold item kept too hot", "02B" = "Hot item kept too cold")) %>% 
  plot_ly(x = ~year_month, y = ~percent_viol, 
          color = ~violation_code, type = "scatter", mode = "lines") %>% 
  layout(xaxis = list(title = "Date"),
         yaxis = list(title = "Percent of total violations"),
         legend = list( x = 0, y = -0.6, title = "Violation type"))
```

### Food temperature related violations over time

```{r}
rest_inspec %>%
  mutate(year = year(inspection_date),
         month = month(inspection_date),
         year_month = make_date(year, month)) %>%
  filter(inspection_date > as.Date("2013-12-01"),
         violation_code == "02G" | violation_code == "02B") %>%
  select(year_month, violation_code) %>%
  group_by(violation_code, year_month) %>%
  summarize(n_obs = n()) %>%
  mutate(violation_code = recode(violation_code, "02G" = "Cold item kept too hot", "02B" = "Hot item kept too cold")) %>%
  plot_ly(x = ~year_month, y = ~n_obs,
          color = ~violation_code, type = "scatter", mode = "lines") %>%
   layout(xaxis = list(title = "Date"),
         yaxis = list(title = "Number of violations"),
         legend = list(x = 0, y = -0.6,
                       title = "Violation type"))
```

### Restuarants with most violations in 2017 in 10001

```{r}
rest_inspec %>% 
  filter(zipcode == 10001) %>%
  mutate(year = year(inspection_date)) %>% 
  filter(year == 2017) %>% 
  count(camis, dba) %>% 
  mutate(dba = as.factor(dba), 
         dba = fct_reorder(dba, -n)) %>% 
  filter(min_rank(desc(n)) < 11) %>% 
  mutate(dba = fct_reorder(dba, n)) %>% 
  plot_ly(y = ~dba, x = ~n, type = "bar", orientation = "h", colors = "viridis") %>% 
  layout(
         xaxis = list(title = "Number of violations"),
         yaxis = list(title = ""))
```