library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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)
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(RColorBrewer)
library(lubridate)
transactions <- transactions_sample

Plot 1

transactions %>%
  inner_join(products, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(product_type) %>%
  filter(kids_count > 0) %>%
  summarise(total_sales = sum(sales_value)) %>%
  arrange(desc(total_sales)) %>% #Top 5 product types that families with kids purchase are GASOLINE-REG UNLEADED, FLUID MILK WHITE ONLY, SOFT DRINKS 12/18&15PK CAN CAR, BEERALEMALT LIQUORS, CHOICE BEEF
  filter(str_detect(product_type, regex("GASOLINE-REG UNLEADED|FLUID MILK WHITE ONLY|SOFT DRINKS 12/18&15PK CAN CAR|BEERALEMALT LIQUORS|CHOICE BEEF", ignore_case = TRUE))) %>%
  ggplot(aes(x = product_type, y = total_sales)) +
  geom_col() +
  scale_x_discrete("Product Types") +
  scale_y_continuous("Total Sales", labels = scales::dollar) +
  ggtitle("Top 5 Products That Families With Kids Purchase",
          subtitle = "Households with one or more children spend the most on gasoline") +
  theme_minimal() +
  coord_flip()

Plot 2

products %>%
  filter(str_detect(product_category, "DOG FOODS")) %>%
  inner_join(transactions, by = 'product_id') %>%
  inner_join(demographics, by = 'household_id') %>%
  ggplot(aes(x = quantity, y = product_type)) +
  geom_point(aes(color = income)) +
  scale_color_brewer(palette="Set3") +
  labs(
    title = "Dog Food Products Quantity Purchased By Income Groups",
    subtitle = "Interestingly, the 50-74K income bracket buys the most gourmet dog foods",
    x = "Quantity Purchased",
    y = "Different Product Types of Dog Foods" ) +
  theme(plot.margin = margin(t = 30, r = 30, b = 30, l = 30, unit = "pt")) +
  theme(axis.text.y = element_text(size = 6))

Plot 3

transactions %>%
  inner_join(products, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  filter(str_detect(product_category, "PIZZA")) %>%
  mutate(transaction_date = date(transaction_timestamp)) %>%
  mutate(transaction_month = month(transaction_timestamp, label = TRUE)) %>%
  group_by(transaction_date, age) %>%
  summarise(total_sales = sum(sales_value)) %>%
  ggplot(aes(x = transaction_date, y = total_sales, color = age)) +
  scale_color_brewer(palette = "Set3") +
  geom_smooth(se = FALSE) +
  scale_y_continuous(name = "Total Sales", labels = scales::dollar) +
  labs(title = "Total Sales of Pizza Products by Age Groups Across The Year",
       subtitle = "Seasonal trends in pizza sales are subtle across age groups during the year",
       x = "Month",
       y = "Total Sales",
       color = "Age")
## `summarise()` has grouped output by 'transaction_date'. You can override using
## the `.groups` argument.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'