Instacart data graphs using plotly

Load necessary data.

data("instacart")

instacart = 
  instacart %>% 
  select(product_id, product_name, aisle, department, reordered) 

plots for the data.

1. Plotly Barchart (top 20 most ordered products)

instacart %>% 
    count(product_name) %>% 
    mutate(rank = min_rank(desc(n))) %>% 
    filter(rank < 20) %>% 
    mutate(product_name = fct_reorder(product_name, desc(n))) %>% 
    plot_ly(
      x = ~product_name, y = ~n,
      color = ~product_name, type = "bar")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

2. Plotly Scatterplot (number of items ordered per aisle for aisles with more than 20,000 items ordered).

instacart %>% 
   count(aisle) %>%
  filter(n > 20000) %>%
  mutate(
    aisle = factor(aisle),
    aisle = fct_reorder(aisle, n)
  ) %>% 
  plot_ly(
    x = ~aisle, y = ~n, alpha = 1,
    type = "scatter", mode = "markers"
  )

3. Plotly Piechart (most reorders by department)

instacart %>% 
  group_by(department) %>% 
  filter(reordered != 0) %>% 
  plot_ly(
    labels = ~department, values = ~reordered, type = "pie"
  )