Load ———————————————————————–

library(ggplot2)
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
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── 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(ggrepel)
library(ggridges)
library(hrbrthemes)
library(sysfonts)
library(showtext)
## Loading required package: showtextdb
data(package = "completejourney")

Plot 1 ————————————————————————–

data("transactions_sample")
data("products")
data("demographics")

Gas sold by Income Levels

top_products <- transactions_sample %>%
  inner_join(products, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(income, product_id, product_type) %>%
  summarise(total_sales = sum(sales_value), total_quantity = sum(quantity), .groups = 'drop') %>%
  arrange(desc(total_sales)) %>%
  group_by(income) %>%
  slice_max(order_by = total_sales, n = 5)

ggplot(top_products, aes(x = total_quantity, y = total_sales, color = income, label = product_type)) +
  geom_point(size = 3, shape = 18, alpha = .5) +
  geom_text_repel() +
  labs(title = "Gas sold by Income Levels",
       subtitle = "Top product",
       x = "Quantity Sold", y = "Revenue") +
  theme_minimal() 
## Warning: ggrepel: 51 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

Plot 2 ————————————————————————–

#Pizzas bought by diff houses

pizza_purchases_raw <- transactions_sample %>%
  inner_join(products, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  filter(str_detect(product_category, "PIZZA"))

ggplot(pizza_purchases_raw, aes(x = as.factor(household_size), y = sales_value, fill = as.factor(household_size))) +
  geom_boxplot() +
  scale_fill_viridis_d(name = "Household Size") +
  labs(title = "Pizzas purchased by Household Size",
       x = "Household Size", y = "Pizzas Sold") +
  theme_minimal()

Plot 3 ————————————————————————–

Favorite category bought by Age

age_category_purchases <- transactions_sample %>%
  inner_join(products, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(age, product_category) %>%
  summarise(total_spent = sum(sales_value), .groups = 'drop') %>%
  arrange(desc(total_spent)) %>%
  group_by(age) %>%
  slice_max(order_by = total_spent, n = 3)

ggplot(age_category_purchases, aes(x = product_category, y = total_spent, fill = age)) +
  geom_col(position = "dodge") +
  labs(title = "Favorite Category by Age",
       x = "Product Category", y = "Amount Sold") +
  theme_minimal() +
  coord_flip()