##Run this code before every visual
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(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(completejourney)
library(tidyverse)
library(dplyr)
library(lubridate)
library(ggplot2)
transactions <- get_transactions()
demographics <- demographics %>%
mutate(income_level = as.character(income))
complete_data <- transactions %>%
inner_join(demographics, by = "household_id") %>%
inner_join(products, by = "product_id")
##Visual 1
products %>%
filter(str_detect(product_category, regex("(MEAT)"))) %>%
inner_join(transactions_sample, by = "product_id") %>%
inner_join(demographics, by = "household_id") %>%
group_by(age) %>%
summarize(total_sales = sum(sales_value, na.rm = TRUE)) %>%
ggplot(aes(x = factor(age), y = total_sales)) +
geom_col() +
scale_y_continuous("Total Sales (Meat)", labels = scales::dollar) +
scale_x_discrete("Age Group") +
ggtitle("Age Group spends most on Meat",
subtitle = "Meat sales per Age Group")
##Visual 2
# Join datasets
complete_data <- transactions %>%
inner_join(demographics, by = "household_id") %>%
inner_join(products, by = "product_id")
# Summarize data for pie chart (count of purchases by age group)
age_summary <- complete_data %>%
group_by(age) %>%
summarize(total_purchases = n(), .groups = 'drop') %>%
mutate(percentage = total_purchases / sum(total_purchases) * 100)
# Create pie chart
pie_chart <- ggplot(age_summary, aes(x = "", y = percentage, fill = age)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y") +
labs(title = "Percentage of Total Purchases by Age Group",
subtitle = "Distribution of Purchases Made Across Different Age Groups",
fill = "Age Group") +
geom_text(aes(label = paste0(round(percentage, 1), "%")),
position = position_stack(vjust = 0.5),
color = "white") + # Adding percentage labels on the pie chart
theme_void() +
theme(legend.position = "right")
# Render the pie chart
print(pie_chart)
##Visual 3
# Filter the products dataset for alcohol-related products
alcohol_products <- products %>%
filter(str_detect(tolower(department), "alcohol") | str_detect(tolower(product_category), "beer|wine|spirits"))
# Join transactions with filtered alcohol products
alcohol_sales <- transactions %>%
inner_join(alcohol_products, by = "product_id")
# Summarize sales value by day of the week
alcohol_sales_by_day <- alcohol_sales %>%
mutate(day_of_week = wday(transaction_timestamp, label = TRUE, abbr = FALSE)) %>% # Extract day of week
group_by(day_of_week) %>%
summarize(total_sales_value = sum(sales_value), .groups = 'drop')
# Create a bar plot for Alcohol Sales Value by Day
alcohol_sales_plot <- ggplot(alcohol_sales_by_day, aes(x = day_of_week, y = total_sales_value)) +
geom_col(fill = "dodgerblue") +
labs(title = "Alcohol Sales Value by Day of the Week",
subtitle = "Total Alcohol Sales for Each Day",
x = "Day of the Week",
y = "Total Sales Value ($)") +
theme_minimal()
# Render the plot
print(alcohol_sales_plot)