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.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ 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(dplyr)
library(ggplot2)
transactions = transactions_sample
view(transactions)
#1 amount of Cerealsales by day of week
products_day <- products %>%
filter(str_detect(product_category, regex("(COLD CEREAL)|(HOT CEREAL)"))) %>%
inner_join(transactions, by = "product_id") %>%
group_by(wday1 = wday(transaction_timestamp, label = TRUE)) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE))
barplot_sales <- ggplot(products_day, aes(x = wday1, y = total_sales, fill = wday1)) +
geom_bar(stat = "identity") +
labs(x = "Day of Week", y = "Total Sales", title = "Total Sales of Cereals by Day of Week") +
scale_fill_brewer(palette = "Set3")
print(barplot_sales)

#2 amount of coupons used by household income
coupon_counts <- coupon_redemptions %>%
group_by(household_id) %>%
summarise(total_coupons = n_distinct(coupon_upc)) %>%
inner_join(demographics, by = "household_id") %>%
filter(!(income %in% c("200-249K", "250K+"))) %>%
select(total_coupons, income)
boxplot <- ggplot(coupon_counts, aes(x = as.factor(income), y = total_coupons, fill = as.factor(income))) +
geom_boxplot() +
scale_fill_brewer(palette = "Set3") +
labs(x = "Household Income", y = "Total Coupons Used", title = "Distribution of Total Coupons Used by Household Income")
print(boxplot)

#3 Vegetables and kids
merged_data <- inner_join(transactions, demographics, by = "household_id")
vegetables_data <- inner_join(merged_data, products, by = "product_id") %>%
filter(str_detect(product_category, "VEGETABLES")) %>%
group_by(kids_count, household_id, product_id) %>%
summarise(total_vegetable_sales = sum(sales_value, na.rm = TRUE))
## `summarise()` has grouped output by 'kids_count', 'household_id'. You can
## override using the `.groups` argument.
scatter_plot <- ggplot(vegetables_data, aes(x = household_id, y = total_vegetable_sales, color = factor(kids_count))) +
geom_point() +
scale_color_brewer(palette = "Set3") +
labs(x = "Household ID", y = "Total Vegetable Sales", title = "Amount of vegetebles bought by Households with a differnt number of kids
") +
labs(color = "Number of Kids") +
theme(legend.position = "bottom")
print(scatter_plot)
