library(tidyverse)
library(p8105.datasets)
library(plotly)
Data Cleaning
data("rest_inspec")
restaurant_inspections =
rest_inspec %>%
select(boro, critical_flag, cuisine_description, inspection_date, inspection_type, score, grade, violation_code, violation_description) %>%
filter(critical_flag == "Critical") %>%
drop_na(grade)
restaurant_inspections
## # A tibble: 102,253 × 9
## boro criti…¹ cuisi…² inspection_date inspe…³ score grade viola…⁴ viola…⁵
## <chr> <chr> <chr> <dttm> <chr> <int> <chr> <chr> <chr>
## 1 MANH… Critic… Americ… 2014-11-13 00:00:00 Pre-pe… 9 A 02G Cold f…
## 2 MANH… Critic… Americ… 2016-06-21 00:00:00 Cycle … 7 A 06D Food c…
## 3 MANH… Critic… Americ… 2017-03-27 00:00:00 Cycle … 12 A 06C Food n…
## 4 MANH… Critic… Korean 2015-02-23 00:00:00 Cycle … 14 B 04L Eviden…
## 5 MANH… Critic… Seafood 2015-05-12 00:00:00 Cycle … 10 A 06D Food c…
## 6 MANH… Critic… Americ… 2016-03-31 00:00:00 Cycle … 13 A 06D Food c…
## 7 MANH… Critic… Korean 2014-12-05 00:00:00 Cycle … 7 A 06C Food n…
## 8 MANH… Critic… Americ… 2014-10-23 00:00:00 Cycle … 7 A 06C Food n…
## 9 MANH… Critic… Café/… 2014-10-01 00:00:00 Cycle … 8 A 02G Cold f…
## 10 MANH… Critic… Pizza/… 2016-08-29 00:00:00 Cycle … 11 A 06B Tobacc…
## # … with 102,243 more rows, and abbreviated variable names ¹critical_flag,
## # ²cuisine_description, ³inspection_type, ⁴violation_code,
## # ⁵violation_description
Make a bar plot
restaurant_inspections %>%
count(cuisine_description) %>%
mutate(cuisine_description = fct_reorder(cuisine_description, n)) %>%
plot_ly(x = ~cuisine_description, y = ~n, color = ~cuisine_description, type = "bar", colors = "viridis") %>%
layout(title = 'NYC Restaurant Cuisine Description')
Make a box plot
restaurant_inspections %>%
mutate(boro = fct_reorder(boro, score)) %>%
plot_ly(y = ~score, color = ~boro, type = "box", colors = "viridis") %>%
layout(title = 'NYC Restaurant Inspections Score by Borough')
Make a pie plot
restaurant_inspections %>%
plot_ly(labels = ~grade, type = 'pie') %>%
layout(title = 'NYC Restaurant Inspections Grade',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))