R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── 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(lubridate)
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
library(readxl)
library(knitr)
library(writexl)

bike_tbl <- read_excel(path = "bikes.xlsx")
bikeshops_tbl <- read_excel(path = "bikeshops.xlsx")
orderlines_tbl <- read_excel(path = "orderlines.xlsx")
## New names:
## • `` -> `...1`
#step 2
kable(head(bike_tbl,5)) 
bike.id model description price
1 Supersix Evo Black Inc. Road - Elite Road - Carbon 12790
2 Supersix Evo Hi-Mod Team Road - Elite Road - Carbon 10660
3 Supersix Evo Hi-Mod Dura Ace 1 Road - Elite Road - Carbon 7990
4 Supersix Evo Hi-Mod Dura Ace 2 Road - Elite Road - Carbon 5330
5 Supersix Evo Hi-Mod Utegra Road - Elite Road - Carbon 4260
glimpse(bike_tbl) 
## Rows: 97
## Columns: 4
## $ bike.id     <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,…
## $ model       <chr> "Supersix Evo Black Inc.", "Supersix Evo Hi-Mod Team", "Su…
## $ description <chr> "Road - Elite Road - Carbon", "Road - Elite Road - Carbon"…
## $ price       <dbl> 12790, 10660, 7990, 5330, 4260, 3940, 3200, 2660, 2240, 18…
#step 3
kable(head(bikeshops_tbl,5))
bikeshop.id bikeshop.name location
1 Pittsburgh Mountain Machines Pittsburgh, PA
2 Ithaca Mountain Climbers Ithaca, NY
3 Columbus Race Equipment Columbus, OH
4 Detroit Cycles Detroit, MI
5 Cincinnati Speed Cincinnati, OH
glimpse(bikeshops_tbl) 
## Rows: 30
## Columns: 3
## $ bikeshop.id   <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1…
## $ bikeshop.name <chr> "Pittsburgh Mountain Machines", "Ithaca Mountain Climber…
## $ location      <chr> "Pittsburgh, PA", "Ithaca, NY", "Columbus, OH", "Detroit…
#step 4
  kable(head(orderlines_tbl,5))
…1 order.id order.line order.date customer.id product.id quantity
1 1 1 2011-01-07 2 48 1
2 1 2 2011-01-07 2 52 1
3 2 1 2011-01-10 10 76 1
4 2 2 2011-01-10 10 52 1
5 3 1 2011-01-10 6 2 1
  glimpse(orderlines_tbl) 
## Rows: 15,644
## Columns: 7
## $ ...1        <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "…
## $ order.id    <dbl> 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7…
## $ order.line  <dbl> 1, 2, 1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2…
## $ order.date  <dttm> 2011-01-07, 2011-01-07, 2011-01-10, 2011-01-10, 2011-01-1…
## $ customer.id <dbl> 2, 2, 10, 10, 6, 6, 6, 6, 6, 22, 8, 8, 8, 8, 16, 16, 16, 1…
## $ product.id  <dbl> 48, 52, 76, 52, 2, 50, 1, 4, 34, 26, 96, 66, 35, 72, 45, 3…
## $ quantity    <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1…
#step 5
  bike_orderlines_joined_tbl <- orderlines_tbl %>% 
  left_join(bike_tbl, by = c("product.id" = "bike.id")) %>% 
  left_join(bikeshops_tbl, by = c("customer.id" = "bikeshop.id")) 
  
#step 6
  ## Separating Description
  bike_orderlines_wrangled_tbl <- bike_orderlines_joined_tbl %>% 
    separate(description,
             into = c("category.1", "category.2", "frame.material"),
             sep = " - ",
             remove = TRUE) %>% 
    
    ## Separating City and State
    
    separate(location,
             into = c("city", "state"),
             sep = ", ",
             remove = FALSE) %>% 
    
    ## Adding Total Price Column
    
    mutate(total.price = price * quantity) %>%
    
    ## Reorganizing/Removing/Reordering/Renaming Columns
    
    select(-"...1", -"location") %>% 
    select(-ends_with(".id")) %>% 
    bind_cols(bike_orderlines_joined_tbl %>% select(order.id)) %>% 
    
    select(contains("date"),contains("id"), contains("order"),
           quantity, price, total.price, everything()) %>% 
    
    rename(order_date = order.date) %>%
    set_names(names(.) %>% str_replace_all("\\.", "_")) 
  
  bike_orderlines_wrangled_tbl %>% glimpse()
## Rows: 15,644
## Columns: 13
## $ order_date     <dttm> 2011-01-07, 2011-01-07, 2011-01-10, 2011-01-10, 2011-0…
## $ order_id       <dbl> 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7…
## $ order_line     <dbl> 1, 2, 1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 3, 4, 1, 2, 3, 4, 1…
## $ quantity       <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1…
## $ price          <dbl> 6070, 5970, 2770, 5970, 10660, 3200, 12790, 5330, 1570,…
## $ total_price    <dbl> 6070, 5970, 2770, 5970, 10660, 3200, 12790, 5330, 1570,…
## $ model          <chr> "Jekyll Carbon 2", "Trigger Carbon 2", "Beast of the Ea…
## $ category_1     <chr> "Mountain", "Mountain", "Mountain", "Mountain", "Road",…
## $ category_2     <chr> "Over Mountain", "Over Mountain", "Trail", "Over Mounta…
## $ frame_material <chr> "Carbon", "Carbon", "Aluminum", "Carbon", "Carbon", "Ca…
## $ bikeshop_name  <chr> "Ithaca Mountain Climbers", "Ithaca Mountain Climbers",…
## $ city           <chr> "Ithaca", "Ithaca", "Kansas City", "Kansas City", "Loui…
## $ state          <chr> "NY", "NY", "KS", "KS", "KY", "KY", "KY", "KY", "KY", "…
#step 7
  sales_by_year_tbl <- bike_orderlines_wrangled_tbl %>% 
    select(order_date, total_price) %>% 
    mutate(year = year(order_date)) %>% 
    group_by(year) %>% 
    summarize(sales = sum(total_price)) %>%
    ungroup() %>%
    mutate(sales_text = scales::dollar(sales))
  
#step 8
  sales_by_year_tbl %>% 
    ggplot(aes(x = year, y = sales))+
    geom_col(fill = "cornflowerblue")+
    geom_label(aes(label = sales_text))+
    geom_smooth(method = "lm", se = FALSE)+
    theme_tq()+
    scale_y_continuous(labels = scales::dollar)+
    labs(
      title = "Bike Shop: Annual Revenue",
      subtitle = "Upward Trend",
      x = "",
      y = "Revenue")
## `geom_smooth()` using formula = 'y ~ x'

#step 9
  sales_by_year_cat_2 <- bike_orderlines_wrangled_tbl %>% 
    select(order_date, total_price, category_2) %>% 
    mutate(year = year(order_date)) %>% 
    group_by(year, category_2) %>% 
    summarize(sales = sum(total_price)) %>%
    ungroup() %>%
    mutate(sales_text = scales::dollar(sales))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
#step 10
  sales_by_year_cat_2 %>% 
    ggplot(aes(x = year, y = sales, fill = category_2))+
    geom_col()+
    facet_wrap(~ category_2, ncol = 3, scales = "free_y")+
    geom_smooth(method = "lm", se = FALSE)+
    theme_tq()+
    scale_fill_tq()+
    scale_y_continuous(labels = scales::dollar)+
    labs(
      title = "Bike Shop: Annual Revenue by Category",
      subtitle = "All Categories are Trending Up",
      x = "",
      y = "Revenue")+
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
## `geom_smooth()` using formula = 'y ~ x'

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.