library(stringr)
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(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.5
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ 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(lubridate)
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
transactions<- get_transactions()
promotions<- get_promotions()

What Are People Buying?

joined_data2 <- transactions %>%
  inner_join(products, by = "product_id")
popular_categories <- joined_data2 %>%
  group_by(product_category) %>%
  summarize(transaction_count = n()) %>%
  arrange(desc(transaction_count))
top_5_categories <- popular_categories %>%
  slice_head(n = 5)
ggplot(top_5_categories, aes(x = reorder(product_category, transaction_count), y = transaction_count)) +
  geom_bar(stat = "identity", fill = "navyblue", color = "black") +
  coord_flip() +
  labs(
    title = "Top Five Product Types",
    x = "Product Type",
    y = "Transactions"
  ) +
  theme_classic()

What Income Levels Are Buying It?

transactions_sample %>%
  inner_join(products) %>%
  inner_join(demographics) %>%
  filter(str_detect(product_type, regex('soft drinks', ignore_case = TRUE))) %>%
  group_by(income) %>%
  summarize(total_soft_drink_purchases = sum(quantity, na.rm = TRUE)) %>%
  ggplot(aes(x=income, y=total_soft_drink_purchases, fill=total_soft_drink_purchases)) + 
  geom_col() + 
  labs(
    title = "Soft Drink Purchase Counts by Annual Income",
    x = "Annual Income",
    y = "Soft Drink Purchases") +  
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
  theme(plot.subtitle = element_text(hjust = 0.5, face = "italic")) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
## Joining with `by = join_by(product_id)`
## Joining with `by = join_by(household_id)`