library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr   1.1.4     ✔ readr   2.1.4
## ✔ forcats 1.0.0     ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4     ✔ tibble  3.2.1
## ✔ purrr   1.0.2     ✔ tidyr   1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(ggplot2)
library(tidyquant)
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'xts'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
## 
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
bike_orderlines_tbl <- read_excel(path = "bike_orderlines.xlsx")
bike_orderlines_tbl %>% select(bikeshop_name, category_1, category_2, quantity) %>% 
  group_by(bikeshop_name, category_1, category_2) %>% 
  summarise(total_quantity = sum(quantity)) %>% 
  ungroup() %>% 
  mutate(bikeshop_name = as_factor(bikeshop_name) %>% 
           fct_reorder(total_quantity)) %>% 
  arrange(desc(total_quantity)) %>% 
  mutate(total_quantity_pct = total_quantity/sum(total_quantity)) %>% 
  mutate(total_quantity_pct_txt = scales::percent(total_quantity_pct))
## `summarise()` has grouped output by 'bikeshop_name', 'category_1'. You can
## override using the `.groups` argument.
## # A tibble: 270 × 6
##    bikeshop_name         category_1 category_2 total_quantity total_quantity_pct
##    <fct>                 <chr>      <chr>               <dbl>              <dbl>
##  1 Kansas City 29ers     Mountain   Cross Cou…            896             0.0444
##  2 Kansas City 29ers     Mountain   Trail                 620             0.0307
##  3 Kansas City 29ers     Mountain   Sport                 558             0.0277
##  4 Denver Bike Shop      Mountain   Cross Cou…            549             0.0272
##  5 Kansas City 29ers     Road       Elite Road            437             0.0217
##  6 Denver Bike Shop      Mountain   Trail                 411             0.0204
##  7 Denver Bike Shop      Mountain   Sport                 388             0.0192
##  8 Oklahoma City Race E… Road       Elite Road            382             0.0189
##  9 Ithaca Mountain Clim… Mountain   Cross Cou…            379             0.0188
## 10 Kansas City 29ers     Road       Endurance…            328             0.0163
## # ℹ 260 more rows
## # ℹ 1 more variable: total_quantity_pct_txt <chr>