Column

Pie Graph: # of orders by Department

Column

Scatter Plot: Average Order Time by Aisles

Bar Plot: # of orders by Aisles

---
title: "Dashboard_Instacart"
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}
data("instacart")

n_aisle = 
  instacart %>% 
  group_by(aisle) %>% 
  summarise(
    n_order = n(),
    average_order_time = round(mean(order_hour_of_day, na.rm = TRUE),1))

n_dep = 
  instacart %>% 
  group_by(department) %>% 
  summarise(n_order = n())
```


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

### Pie Graph: # of orders by Department

```{r}
n_dep %>%  
  mutate(department = fct_reorder(department, n_order)) %>%
  plot_ly(labels = ~department, values = ~n_order, type = "pie", 
    textinfo = "label+percent", colors = "viridis", alpha = 0.75)
```

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

### Scatter Plot: Average Order Time by Aisles

```{r}
n_aisle %>% 
  mutate(text_label = str_c("Time: ", average_order_time, "\nAisle: ", aisle, "\n# of Orders: ", n_order)) %>% 
  plot_ly(x = ~average_order_time, y = ~aisle, type = "scatter", mode = "markers",
    color = ~aisle, text = ~text_label, colors = "viridis", alpha = 0.75)
```

### Bar Plot: # of orders by Aisles

```{r}
n_aisle %>% 
  filter(n_order > 10000) %>% 
  mutate(aisle = fct_reorder(aisle, n_order)) %>%
  plot_ly(
    x = ~n_order, y = ~aisle, type = "bar", color = ~aisle, 
    colors = "viridis", alpha = 0.75)
```