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.4 ✔ 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.
transactions <- get_transactions()
transactions
demographics <- completejourney::demographics
products <- completejourney::products
fruit_veg <- products %>%
filter(str_detect(product_category, "VEGETABLES|FRUIT"))
fruit_veg_purchases <- transactions %>%
inner_join(fruit_veg, by = "product_id") %>%
inner_join(demographics, by = "household_id")
marital_purchases <- fruit_veg_purchases %>%
group_by(marital_status) %>%
summarise(count = n()) %>%
mutate(percentage = count / sum(count) * 100) %>%
arrange(desc(count))
ggplot(marital_purchases, aes(x = marital_status, y = percentage, fill = marital_status)) +
geom_col(color = "black") +
labs(title = "Proportion of Fruit & Vegetable Purchases by Marital Status",
x = "Marital Status",
y = "Percentage of Purchases (%)") +
theme_minimal() +
scale_fill_brewer(palette = "Set2")

vegetables <- products %>%
filter(str_detect(product_category, "VEGETABLES"))
vegetable_purchases <- transactions %>%
inner_join(vegetables, by = "product_id")
vegetable_purchases <- vegetable_purchases %>%
mutate(purchase_time = hms:: as_hms(transaction_timestamp),
purchase_hour = hour(transaction_timestamp))
hourly_purchases <- vegetable_purchases %>%
group_by(purchase_hour) %>%
summarise(count = n()) %>%
arrange(desc(count))
ggplot(hourly_purchases, aes(x = purchase_hour, y= count)) +
geom_col(fill = "lightgreen", color = "black") +
scale_x_continuous(breaks = seq(0,23, by = 1)) +
labs(title = "Most Popular Time to Buy Vegetables",
x = "Hour of the Day",
y= "Number of Transactions") +
theme_minimal()

household_purchases <- transactions %>%
group_by(household_id) %>%
summarise(total_quantity = sum(quantity))
age_quantity_data <- demographics %>%
select(household_id, age) %>%
inner_join(household_purchases, by = "household_id")
ggplot(age_quantity_data, aes(x = age, y = total_quantity, color = age)) +
geom_jitter(width = 0.2, alpha = 0.6) + # Jitter points to prevent overlap
labs(title = "Relationship Between Age and Quantity of Products Bought",
x = "Age Range",
y = "Total Quantity of Products Bought") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
