library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
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.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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(lubridate)
library(ggplot2)
library(dplyr)

Plot 1

this plot shows the top 10 products bought by the top 100 spenders (households)

Y <- products
X <- transactions_sample
top_hsld <- X %>%
  group_by(household_id) %>%
  summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  filter(total_sales >= 324.47) 
X1 <- inner_join(X, top_hsld, by = "household_id")
X2 <- inner_join(X1,Y, by = "product_id")

X3 <- X2 %>% filter(total_sales >=324.47) %>% arrange(desc(total_sales))

X4 <-  X3 %>%
  group_by(product_type) %>%
  summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  arrange(desc(total_sales)) %>%
  slice(1:10) 
X4$product_type[X4$product_type=="GASOLINE-REG UNLEADED"]<-"GAS"
X4$product_type[X4$product_type=="FLUID MILK WHITE ONLY"]<-"MILK"
X4$product_type[X4$product_type=="SOFT DRINKS 12/18&15PK CAN CAR"]<-"SODA"
X4$product_type[X4$product_type=="BEERALEMALT LIQUORS"]<-"ALCOHOL"
X4$product_type[X4$product_type=="CHOICE BEEF"]<-"BEEF"
X4$product_type[X4$product_type=="TOILET TISSUE"]<-"TOILET PAPER"
X4$product_type[X4$product_type=="LIQUID LAUNDRY DETERGENTS"]<-"LAUNDRY DETERGENT"

Plot 2

Plot 3

Plot 3 shows the top 10 products bought by quantity (for spenders earning less than $75k)

LOW_INCOME <- c("35-49K", "50-74K","25-34K","15-24K","Under 15K")

W9 <- inner_join(dEMO,X3, by="household_id") %>%
  filter(income %in% LOW_INCOME) %>%
group_by(product_category) %>%
  summarise(total_quan = sum(quantity, na.rm = TRUE)) %>%
  arrange(desc(total_quan)) %>%
  filter(total_quan < 10000) %>%
  slice(1:10)



W9$product_category[W9$product_category=="FLUID MILK PRODUCTS"]<-"MILK"
W9$product_category[W9$product_category=="VEGETABLES - SHELF STABLE"]<-"VEGETABLES"
W9$product_category[W9$product_category=="BAG SNACKS"]<-"SNACKS"
W9$product_category[W9$product_category=="FRZN MEAT/MEAT DINNERS"]<-"FROZEN MEAT"