Top 5 brands by transaction volume
Transaction count for households with and without kids
library(completejourney)
library(ggplot2)
library(dplyr)
data("transactions_sample")
data("products")
data("demographics")
full_data <- transactions_sample %>%
left_join(demographics, by = "household_id") %>%
left_join(products, by = "product_id") %>%
filter(!is.na(kids_count)) %>%
mutate(has_kids = ifelse(kids_count > 0, "With Kids", "Without Kids"))
# Summarizing total spending by brand and household type :
specific_brands <- c("COCA-COLA", "PEPSI", "NESTLE", "KELLOGG", "P&G")
brand_spending_data <- full_data %>%
filter(brand %in% specific_brands & !is.na(brand)) %>%
group_by(has_kids, brand) %>%
summarize(total_spend = sum(sales_value, na.rm = TRUE)) # Total spending
# Top 5 Brands by Transaction Volume `(Households with vs. without kids)`
top_brands_data <- full_data %>%
filter(!is.na(brand)) %>%
group_by(has_kids, brand) %>%
summarize(total_transactions = n()) %>%
arrange(has_kids, desc(total_transactions)) %>%
group_by(has_kids) %>%
slice_max(total_transactions, n = 5) # Top 5 brands per group
ggplot(top_brands_data, aes(x = reorder(brand, total_transactions), y = total_transactions, fill = has_kids)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("With Kids" = "orange", "Without Kids" = "brown")) + coord_flip() +labs(title = "Top 5 Brands by Transaction Volume", subtitle = "Comparing top brands by transaction count for households with and without kids",
x = "Brand", y = "Number of Transactions", fill = "Household Type") +
theme_minimal()

Sales Trend for Top 5 Brands Over Weeks by Age
Group
# Merge transactions_sample with products and demographics to get brand and age information
merged_data <- transactions_sample %>%
left_join(products, by = "product_id") %>%
left_join(demographics, by = "household_id") %>%
filter(!is.na(brand), !is.na(age))
# Identify top 5 brands by total sales value
top_brands <- merged_data %>%
group_by(brand) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
top_n(5, total_sales) %>%
pull(brand)
# Filter data for the top 5 brands and group by week, brand, and age
top_brands_sales_trend <- merged_data %>%
filter(brand %in% top_brands) %>%
group_by(week, brand, age) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE), .groups = 'drop')
# Plot the sales trend for the top 5 brands over weeks, colored by brand, faceted by age
ggplot(top_brands_sales_trend, aes(x = week, y = total_sales, color = brand)) +
geom_line() +
facet_wrap(~age, scales = "free_y") + labs(title = "Sales Trend for Top 5 Brands Over Weeks by Age Group",
x = "Week",
y = "Total Sales Value ($)",
color = "Brand") +
theme_minimal() +
theme(legend.position = "top")

Total Sales by Income, Household Size, and Marital
Status
# Load the libraries that are needed for the plot:
library(dplyr)
library(ggplot2)
# Load the data sets for plotting:
data("transactions_sample")
data("demographics")
# Joining the data sets
merged_data <- transactions_sample %>%
left_join(demographics, by = "household_id") %>%
filter(!is.na(household_size), !is.na(income), !is.na(sales_value), !is.na(marital_status))
# A summary for total sales by income, household size, and marital status:
heatmap_data <- merged_data %>%
group_by(income, household_size, marital_status) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE), .groups = "drop")
# Creating heatmap with facets for marital status :
ggplot(heatmap_data, aes(x = as.factor(household_size), y = income, fill = total_sales)) +
geom_tile(color = "white") + scale_fill_gradient(low = "lightblue", high = "darkblue", name = "Total Sales ($)") + # Color gradient
labs(title = "Heatmap of Total Sales by Income, Household Size, and Marital Status",
x = "Household Size",
y = "Income ($)",
subtitle = "Darker colors represent higher sales values") +
facet_wrap(~ marital_status) + theme_minimal() +theme(plot.title = element_text(face = "bold", size = 12),
plot.subtitle = element_text(size = 12, margin = margin(b = 10)),
axis.title = element_text(face = "bold"),
strip.text = element_text(face = "bold")) # Bold facet labels
