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.
transactions<- transactions_sample
filter_by_kid_count<- transactions %>%
  inner_join(demographics, by = join_by(household_id)) %>%
  filter(kids_count >0) %>%
  group_by(kids_count) %>%
  summarize(total_sales = sum(sales_value)) 
ggplot(filter_by_kid_count,aes(kids_count, total_sales)) +
  geom_bar(stat = "identity")+
  labs(title = "Total Sales by Kid Count",
       x = "Kid Count",
       y = "Total Sales Value"
       )

meat_department<- transactions%>%
  inner_join(products)%>%
  filter(str_detect(department, 'MEAT'))%>%
  mutate(month = month(transaction_timestamp)) %>%
  group_by(month)%>%
  summarize(total_sales = sum(sales_value))
## Joining with `by = join_by(product_id)`
ggplot(meat_department, aes(month, total_sales))+
  geom_line(color = 'blue', alpha = .50)+
  scale_x_continuous(breaks = 1:12 )+
  labs(title = "Total Monthly Meat Sales", x = "Month #", y = "Sales Value")

product_income <- transactions %>%
  inner_join(demographics)%>%
  inner_join(products) %>%
  group_by(income, product_type) %>%  
  summarize(total_sales = sum(sales_value, na.rm = TRUE), .groups = 'drop') %>%
  arrange(income, desc(total_sales))
## Joining with `by = join_by(household_id)`
## Joining with `by = join_by(product_id)`
top_products_income <- product_income%>%
  group_by(income)%>%
  slice_max(total_sales, n = 1)
ggplot(top_products_income, aes(x = reorder(product_type, total_sales), y = total_sales, fill = income)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Top Product(GASOLINE-REG UNLEADED)by Total Sales Across Income Ranges",
       x = "Product Name",
       y = "Total Sales Value",
       fill = "Income Range")