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.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.
## Visualization 1
products %>%
filter(str_detect(product_category, regex("(VITAMINS)"))) %>%
inner_join(transactions_sample, by = "product_id") %>%
inner_join(demographics, by = "household_id") %>%
group_by(age) %>%
summarize(total_sales = sum(sales_value, na.rm = TRUE)) %>%
ggplot(aes(x = factor(age), y = total_sales)) +
geom_col() +
scale_y_continuous("Total Sales for Vitamins", labels = scales::dollar) +
scale_x_discrete("Age") +
ggtitle("Which Age Group Spends the Most on Vitamins?",
subtitle = "People aged 45-54 spend the most money on Vitamins")

## Visualization 2
demographics %>%
inner_join(completejourney::transactions_sample, by = "household_id") %>%
group_by(marital_status, income) %>%
summarize(total_retail_disc = sum(retail_disc, na.rm = TRUE)) %>%
ggplot(aes(x = marital_status, y = total_retail_disc)) +
geom_col() +
scale_y_continuous("Total Retail Discount", labels = scales::dollar) +
scale_x_discrete("Marital Status") +
facet_wrap(~income) +
ggtitle("Who uses the most Retail Discounts?",
subtitle = "Unmarried Households with income ranging from $50-74k use the most retail discounts")
## `summarise()` has grouped output by 'marital_status'. You can override using
## the `.groups` argument.

## Visualization 3
demographics %>%
inner_join(completejourney::transactions_sample, by = "household_id") %>%
group_by(household_size, income) %>%
summarize(total_coupon_disc = sum(coupon_disc, na.rm = TRUE)) %>%
ggplot(aes(x = household_size, y = total_coupon_disc)) +
geom_col() +
scale_y_continuous("Total Coupon Discount", labels = scales::dollar) +
scale_x_discrete("Household Size") +
facet_wrap(~income) +
ggtitle("Which household size uses the most coupons?",
subtitle = "Households sizes of 2 with income in the range of 50-74k use the most coupons ")
## `summarise()` has grouped output by 'household_size'. You can override using
## the `.groups` argument.
