library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(dplyr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## 
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
transactions <- as.data.frame(get_transactions())
trans1 <- as.data.frame(transactions_sample)
# Products which have PIZZA in their category, according to age and product type
transactions %>%
  inner_join(demographics, by = "household_id") %>%
  inner_join(products, by = "product_id") %>%
  filter(str_detect(product_category, "PIZZA")) %>%
  ggplot(aes(x = age, y = sales_value, color = product_type)) +
  geom_point() +
  scale_x_discrete(guide = guide_axis(n.dodge=2), name = "Age") +
  scale_y_continuous(name= "Total Sales") +
  ggtitle("Products with PIZZA category, according to age and product type")

# Facet - Monthly total sales vs kids count / Household_Size
transactions <- transactions %>%
  mutate(Month = month(transaction_timestamp, label = TRUE))

transactions %>%
  inner_join(demographics, by = "household_id") %>%
  inner_join(products, by = "product_id") %>%
  filter(str_detect(product_category, "PIZZA")) %>%
  group_by(household_size) %>%
  ggplot(aes(y = sales_value, x = household_size, color = age)) + geom_point() + 
  facet_wrap(~ Month) +
  scale_x_discrete(guide = guide_axis(n.dodge=2), name = "Household Size") +
  scale_y_continuous(name = "Sales") +
  ggtitle("Monthly Sales VS Household Size")

# Top 20 product categories according to total sales
transactions %>%
  inner_join(products, by = "product_id") %>%
  group_by(product_category) %>%
  summarise(TotalSales = sum(sales_value)) %>%
  arrange(desc(TotalSales)) %>%
  slice(1:20) %>%
  ggplot(aes(y = TotalSales, x = product_category)) + 
  geom_bar(color = "blue", stat="Identity") + 
  theme(axis.text.x = element_text(angle = 60, hjust= 1)) +
  scale_x_discrete(name = "Product Category") +
  scale_y_continuous(name= "Total Sales") +
  ggtitle("Top 20 Product Categories according to Total Sales")