library(ggplot2)
library(dplyr)  
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(stringr)
transactions <- transactions_sample
##Plot 1
transactions_sample %>%
  inner_join(demographics, by = 'household_id') %>%
  group_by(age,household_comp) %>%
  summarize(total_quantity = sum(quantity)) %>%
  ggplot(aes(x = age, y = total_quantity, fill = household_comp)) +
  geom_bar(stat = "identity") +
  guides(fill = guide_legend(title = "Household Composition")) +
  scale_y_continuous(breaks = seq(0,1600000, by = 400000)) +
  labs(title = "Total Quantity Purchased by each Age Group classified by Household composition",
       x = "Age Group",
       y = "Total Quantity Purchased")
## `summarise()` has grouped output by 'age'. You can override using the `.groups`
## argument.

##Plot 2
transactions_sample %>%
  inner_join(demographics, by = 'household_id') %>%
  inner_join(products, by = 'product_id') %>%
  filter(department == 'DRUG GM') %>%
  mutate(loyalty_price = (sales_value + coupon_match_disc) / quantity) %>%
  filter(loyalty_price > 0) %>%
  ggplot(aes(x = age , y = loyalty_price , fill = age)) +
  geom_col() +
  scale_y_continuous(limits = c(0, 6500), breaks = seq(0, 6500, by = 500)) +
  labs(
    title = "Loyalty Price Per Age Group for DRUG GM Department",
    x = "Age Group",
    y = "Loyalty Price"
  ) + theme(legend.position = "none")

##Plot 3
products %>%
  filter(str_detect(product_type, "PICKLES")) %>%
  inner_join(transactions_sample , by = 'product_id') %>%
  inner_join(demographics , by = 'household_id') %>%
  ggplot(aes(x = quantity , y = product_type)) +
  geom_point(aes(color = income)) +
  facet_wrap(~ age) +
  scale_color_brewer(palette="Set3") +
  labs(
    title = "Pickle Products Quantity Purchased By Different Age-Groups",
    x = "Quantity Purchased",
    y = "Different Product Types of Pickles" ) +
  guides(color = guide_legend(title = "Income Group"))