# Load necessary libraries
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
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
# Set global options for chunk output
knitr::opts_chunk$set(echo = TRUE, fig.show = "hold")
install.packages("completejourney")
## Warning: package 'completejourney' is in use and will not be installed
# Load necessary libraries
library(completejourney)
library(dplyr)
library(ggplot2)
library(scales)
# Load the transactions dataset
transactions <- get_transactions()
promotions <- get_promotions()
# Check if 'transactions' is loaded
exists("transactions")
## [1] TRUE
exists("promotions")
## [1] TRUE
# Merge datasets
merged_data <- transactions %>%
left_join(products, by = "product_id") %>%
left_join(demographics, by = "household_id")
# PLOT 1
# Merge the data
merged_data <- transactions %>%
left_join(products, by = "product_id") %>%
left_join(demographics, by = "household_id")
# Filter to show only top 5 product categories by total sales
top_5_categories <- merged_data %>%
group_by(product_category) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE), .groups = 'drop') %>% # Specify .groups
arrange(desc(total_sales)) %>%
slice(1:5) # Get top 5 categories
# Filter the main dataset based on top 5 categories
filtered_data <- merged_data %>%
filter(product_category %in% top_5_categories$product_category) %>%
group_by(product_category, income) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE), .groups = 'drop') # Specify .groups
# Create the plot with formatted y-axis range
ggplot(filtered_data, aes(x = product_category, y = total_sales, fill = income)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(labels = scales::comma, limits = c(0, 75000)) + # Set y-axis range from 0 to 75,000
labs(
title = "Top 5 Product Categories by Income Level",
subtitle = "Total sales value for the top 5 product categories by income level",
x = "Product Category",
y = "Total Sales Value"
) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-axis labels
axis.text.y = element_text(size = 12), # Adjust y-axis text size
plot.title = element_text(hjust = 0.5), # Center the title
plot.subtitle = element_text(hjust = 0.5) # Center the subtitle
)
## Warning: Removed 3 rows containing missing values or values outside the scale range
## (`geom_bar()`).

##PLOT 2
# Join transactions with demographics and products
merged_data <- transactions %>%
left_join(products, by = "product_id") %>%
left_join(demographics, by = "household_id")
# Aggregate to get total sales_value per quantity for top 5 products for each income level
top_5_products_income <- merged_data %>%
group_by(income, product_id, product_category) %>%
summarize(total_sales = sum(sales_value), total_quantity = sum(quantity)) %>%
arrange(desc(total_sales)) %>%
top_n(5, total_sales)
## `summarise()` has grouped output by 'income', 'product_id'. You can override
## using the `.groups` argument.
# Aggregate total sales per household type
sales_by_household_type <- merged_data %>%
group_by(household_size, household_comp) %>%
summarize(total_sales = sum(sales_value)) %>%
filter(!is.na(household_size))
## `summarise()` has grouped output by 'household_size'. You can override using
## the `.groups` argument.
# Plot: Total Sales per Household Type
ggplot(sales_by_household_type, aes(x = household_size, y = total_sales, fill = household_comp)) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Total Sales per Household Comp",
subtitle = "Breakdown by household size and household Comp",
x = "Household Size",
y = "Total Sales Value"
) +
theme_minimal()

##PLOT 3
# Step 1: Filter top 5 product categories by total sales
top_categories_3 <- merged_data %>%
group_by(product_category) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
arrange(desc(total_sales)) %>%
slice(1:5) # Get top 5
# Step 2: Filter the main dataset
filtered_data_3 <- merged_data %>%
filter(product_category %in% top_categories_3$product_category) %>%
group_by(product_category) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE), .groups = 'drop') # Recalculate total_sales for filtered data
# Step 3: Create the plot with x-axis format as whole numbers
ggplot(filtered_data_3, aes(x = total_sales, y = reorder(product_category, total_sales), fill = product_category)) +
geom_bar(stat = "identity") +
scale_x_continuous(labels = scales::number_format()) + # Format x-axis as whole numbers
labs(
title = "Total Sales by Product Category",
subtitle = "Visualizing total sales for selected product categories",
x = "Total Sales",
y = "Product Category"
) +
theme_minimal() +
theme(
axis.text.x = element_text(size = 12), # Adjust x-axis text size
axis.text.y = element_text(size = 12), # Adjust y-axis text size
plot.title = element_text(hjust = 0.5), # Center the title
plot.subtitle = element_text(hjust = 0.5)
)
