# All Library functions for packages
library(completejourney)
## Warning: package 'completejourney' was built under R version 4.2.3
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ 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(ggplot2)

# Data variables
prod <- products
transactions <- get_transactions()
d <- demographics
prom <- get_promotions()
c <- coupons
prod %>%
  filter(product_category == "COFFEE")%>%
  count(product_type)
## # A tibble: 17 × 2
##    product_type                       n
##    <chr>                          <int>
##  1 AUTHENTIC DRY BEVERAGES W/O SW     1
##  2 DECAF BEAN: FLAVORED               3
##  3 DECAF BEAN: REGULAR               11
##  4 FLAVORED BEAN COFFEE              30
##  5 GROUND COFFEE                    186
##  6 GROUND DECAFFINATED               35
##  7 GROUND FLVR DECAF COFFEE          19
##  8 GROUND FLVR REG COFFEE            56
##  9 INSTANT COFFEE FLAVORED NO SWE    63
## 10 INSTANT COFFEE FLAVORED W/SWEE    37
## 11 INSTANT COFFEE REGULAR            30
## 12 INSTANT DECAF FLVR COFFEE W/ S     5
## 13 INSTANT DECAFFINATED              12
## 14 INSTANT TEA & TEA MIX              1
## 15 MOLASSES & SYRUPS                  2
## 16 NON DAIRY CREAMER: DRY            67
## 17 REGULAR BEAN                      46
# First graph
transactions %>%
  inner_join(prod, by = "product_id")%>%
  inner_join(d, by = "household_id")%>%
  mutate(total_sales = sum(sales_value))%>%
  filter(department == "GROCERY")%>%
  ggplot()+
  geom_col(aes(x = department, y = total_sales, fill = income))+
  labs(title = "Which Income Range Purchases the Most Groceries?",
    subtitle = "Grocery Sales by Income Range",
       x = "",
       y = "Total Sales")+
  scale_y_continuous(labels = c("0","250 Billion", "500 Billion", "750 Billion", "1 Trillion"),
                breaks = c(0,250000000000, 500000000000,750000000000, 1000000000000))

# Second Graph
transactions %>%
  inner_join(prod, by = "product_id")%>%
  inner_join(d, by = "household_id")%>%
  mutate(month = month(transaction_timestamp))%>%
  mutate(total_sales = sum(sales_value))%>%
  ggplot()+
  geom_col(aes(x = month, y = total_sales, fill = age))+
  scale_x_continuous(name = "Month",
                     labels = c("Jan", "Feb", "Mar", "Apr", "May",
                              "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
                   breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))+
  scale_y_continuous(name = "Total Sales",
    labels = c("0", "50 Billion", "100 Billion", "150 Billion", "200 Billion"),
                     breaks = c(0, 50000000000, 100000000000, 150000000000, 200000000000))+
  ggtitle("Total Monthly Sales by Age Group")

# Third Graph
d %>%
  inner_join(transactions, by = "household_id")%>%
  mutate(total_sales = sum(sales_value))%>%
  na.omit(marital_status)%>%
  ggplot()+
  geom_col(aes(x = household_size, y = total_sales, fill = marital_status))+
  labs(title = "Total Sales by Household Size and Marital Status",
       x = "Household Size",
       y = "Total Sales",
       fill = "Marital Status")+
  scale_y_continuous(labels = c("0", "200 Billion", "400 Billion"),
                     breaks = c(0, 200000000000, 400000000000) )