## ── 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
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.

Sales value by household comp ———————————————————

join1 <- tran %>%
  inner_join(demo, by = "household_id") %>%
  select(household_comp, sales_value)
sales_by_comp <- join1 %>%
  group_by(household_comp) %>%
  summarise(total_sales = sum(sales_value))
plot1 <- ggplot(sales_by_comp, aes(x = household_comp, y = total_sales, fill = household_comp)) +
  geom_bar(stat = "identity") +
  labs(title = "Total Sales Value by Household Composition",
       x = "Household Composition",
       y = "Total Sales Value")
plot(plot1)

Income by marital status ——————————————————————

join2 <- demo %>%
  inner_join(tran, by = "household_id") %>%
  select(marital_status, income)
join2$marital_status[is.na(join2$marital_status)] <- "Unknown"
filter2 <- join2 %>%
  filter(marital_status != "Unknown")
plot2 <- ggplot(join2,  aes(x = income, fill = marital_status)) +
  geom_density(alpha = 0.5) +
  labs(title = "Distribution of Household Incomes by Marital Status",
       x = "Household Income",
       y = "Density",
       fill = "Marital Status")
plot(plot2)

Sales by product category ——————————————————————

join3 <- tran %>%
  inner_join(prod, by = "product_id") %>%
  select(product_category, sales_value)
sales_by_category <- join3 %>%
  group_by(product_category) %>%
  summarise(total_sales = sum(sales_value))
sales_by_category <- sales_by_category[order(sales_by_category$total_sales, decreasing = TRUE), ]
plot3 <- ggplot(sales_by_category, aes(x = reorder(product_category, -total_sales), y = total_sales)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "Distribution of Sales Values by Product Category",
       x = "Product Category",
       y = "Total Sales Value")
plot(plot3)