Column

Violin plot

Column

Bar chart

Scatter plot

---
title: "Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source: embed
---

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

```{r import and clean}
data("rest_inspec")

rest_inspec_clean = rest_inspec %>%
  janitor::clean_names() %>% 
  mutate(cuisine_description = gsub("[[:punct:]].*","",cuisine_description)) %>%
  mutate(cuisine_description = recode(cuisine_description,
    "CafÃ" = "Cafe"
  ))

# 20 most frequent restaurant categories
freq_cuisine_20 = count(rest_inspec_clean, cuisine_description) %>%
  arrange(desc(n)) %>%
  slice(1:20)

# restaurant rating distribution for the most frequent categories
rest_ratings_20 = rest_inspec_clean %>%
  filter(!is.na(score)) %>%
  filter(cuisine_description %in% freq_cuisine_20$cuisine_description)

# restaurant critical versus non critical for the most frequent categories
rest_crit_not_crit_20 = rest_inspec_clean %>%
  select(cuisine_description, critical_flag) %>%
  filter(cuisine_description %in% freq_cuisine_20$cuisine_description) %>%
  group_by(cuisine_description) %>%
  summarise(
    critical = sum(critical_flag == "Critical"),
    not_critical = sum(critical_flag == "Not Critical"))
```

Column {data-width=650}
-----------------------------------------------------------------------

### Violin plot

```{r violin plot}
plot_ly(rest_inspec_clean %>% filter(boro!="Missing"),
        y = ~score, color = ~boro,
        type = "violin", 
        colors = "viridis") %>%
  layout(title = 'Inspection Scores by Borough',
        xaxis = list(title = 'Borough'),
        yaxis = list(title = 'Inspection Scores'))
```


Column {data-width=350}
-----------------------------------------------------------------------

### Bar chart

```{r bar chart}
plot_ly(freq_cuisine_20,
        x = ~reorder(cuisine_description,-n), y = ~n, color = ~cuisine_description,
        type = "bar", 
        colors = "viridis",
        text = ~cuisine_description) %>%
  layout(title = 'Restaurants by Cuisine',
        xaxis = list(title = 'Cuisine Type'),
        yaxis = list(title = 'Restaurants'))
```

### Scatter plot

```{r scatter plot}
plot_ly(rest_crit_not_crit_20,
        x = ~not_critical, y = ~critical, color = ~cuisine_description,
        alpha = 0.7, type = "scatter", 
        mode = "markers", colors = "viridis",
        text = ~cuisine_description) %>%
  layout(title = 'Critical vs Non-critical Violations by Cuisine',
        xaxis = list(title = 'Non-critical Violations'),
        yaxis = list(title = 'Critical Violations'))
```