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(ggplot2)
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(tidyr)
products = completejourney::products
transactions = completejourney::transactions_sample
coupons = completejourney::coupons
coupon_redemptions = completejourney::coupon_redemptions
demographics = completejourney::demographics
First Plot
combined_data <- transactions %>%
left_join(demographics, by = "household_id") %>%
left_join(products, by = "product_id")
top_products_by_income <- combined_data %>%
group_by(income, home_ownership) %>%
summarize(total_sales_value = sum(sales_value), total_discount = sum(retail_disc)) %>%
arrange(income, desc(total_sales_value)) %>%
group_by(income) %>%
top_n(5)
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.
## Selecting by total_discount
custom_colors <- c("Renter" = "blue",
"Probable Renter" = "green",
"Homeowner" = "pink",
"Probable Homeowner" = "red",
"NA" = "gray")
ggplot(top_products_by_income, aes(x = income, y = total_sales_value - total_discount, fill = home_ownership)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Total Dollars Saved on Discounts per Income Level",
subtitle = "How much is Saved on Groceries based on Home Ownership?",
x = "Income Level", y = "Total Money Saved") +
scale_fill_manual(values = custom_colors) +
theme_minimal() +
theme(legend.position = "top") +
guides(fill = guide_legend(title = "Product")) +
scale_y_continuous(trans = 'log2')
