library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
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
transactions <- transactions_sample
transactions %>%
  mutate(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  inner_join(demographics) %>%
  filter(
    (marital_status == "Unmarried"),
    (home_ownership == "Homeowner")) %>%
  ggplot(aes(x = income, y = total_sales)) + geom_col(color = "navyblue", width = 0.6) +
  scale_x_discrete(name = "Income Range") +
  scale_y_continuous(name = "Sales ($)", labels = scales::dollar) +
  theme(axis.text.x = element_text(angle = 30), axis.title.y = element_text(vjust = 3.5)) +
  ggtitle("Total Sales by Income of Unmarried Homeowners")
## Joining with `by = join_by(household_id)`

graph_2 <- products %>%
 inner_join(transactions) %>%
  inner_join(demographics) %>%
  filter(kids_count >= "3+") %>%
  mutate(product_count = nchar(product_category)) %>%
  select(c(product_count, kids_count, product_category)) %>%
  arrange(desc(product_count)) %>%
  unique() %>%
  head(product_count, n = 10)
## Joining with `by = join_by(product_id)`
## Joining with `by = join_by(household_id)`
graph_2 %>%
  ggplot(aes(x = " ", y = product_count, fill = product_category)) +
  geom_col(width = 1.5, stat = "identity") +
  scale_x_discrete(name = "") +
  scale_fill_discrete(name = "Product Category") +
  scale_y_discrete(name = "") +
  theme(axis.text.y = element_blank(), axis.text.x = element_blank()) +
  ggtitle("Top 10 Products for Households with 3+ Kids")
## Warning in geom_col(width = 1.5, stat = "identity"): Ignoring unknown
## parameters: `stat`

graph_3 <- transactions %>%
  inner_join(products) %>%
  mutate(day = lubridate::wday(transaction_timestamp, label = TRUE)) %>%
  mutate(total_sales = sum(sales_value)) %>%
  filter(product_category == "ICE CREAM/MILK/SHERBTS", ignore_case = TRUE)
## Joining with `by = join_by(product_id)`
graph_3 %>%
  ggplot(aes(x = day, y = total_sales)) + geom_col(fill = "plum", color = "coral", width = 0.7) +
  scale_x_discrete(name = "Day of Week") +
  scale_y_continuous(name = "Total Sales ($)", labels = scales::dollar) +
  theme(axis.title.y = element_text(vjust = 3.2),axis.title.x = element_text(vjust = 0.4)) +
  ggtitle("Total Sales of Ice Cream by Day of Week")