library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── 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.
options(scipen=1000000)

transactions <- get_transactions()
joined_data <- inner_join(transactions, demographics, by = "household_id")
aggregated_data <- joined_data %>%
  group_by(income, age) %>%
  summarise(total_sales_value = sum(sales_value), total_quantity = sum(quantity)) %>%
  mutate(total_revenue = total_sales_value*total_quantity) %>%
  arrange(income) 
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.
g1 <- ggplot(aggregated_data, aes(x = income, y = total_revenue, fill = age)) +
  geom_col() +
  labs(title = "Revenue Generated by Choices of Consumers by Income and Age",
      x = "Consumers' Income",
      y = "Revenue",
       fill = "Comsumers' Age") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
g1 <- g1 + labs(subtitle = "This graph shows an overview of consumers' choice of sales value and quantity of products based on their \nincome and age, thus concludes the total revenue generated from those purchases.  It is clear \nthat consumers of age group 45-54 with income range of $50-74K contributed the most \nto the store's revenue, which was over $2 trillion.")
g1 <- g1 + theme(plot.subtitle = element_text(size = 8, color = "darkslategrey"))

print(g1)

prod_trans <- inner_join(transactions, products, by = "product_id")
data1 <- prod_trans %>%
  group_by(department, sales_value) %>%
  arrange(desc(transaction_timestamp)) %>%
  filter(sales_value >= 100.00)

data1$transaction_timestamp <- as.Date(data1$transaction_timestamp)
g2 <- ggplot(data1, aes(x = transaction_timestamp, y = sales_value, color = department)) +
  geom_point() +
  geom_smooth(alpha = 0.2) +
  scale_x_date(date_labels = "%b %Y") +
  labs(title = "Top Product Deparments that Generate the Largest Amount of Sales",
      x = "Timestamp",
      y = "Sales",
      color = "Product Department") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
g2 <- g2 + labs(subtitle = "This graph shows the top product departments that generated the largest amount of sales (>= $100) \nand visualizes their sales from Jan 2017 to Dec 2017. From that, Miscellaneous Department \nbrought the highest sales in Feb, but Drug GM Department generated the most \nstable and consistent sales throughout the entire period.")
g2 <- g2 + theme(plot.subtitle = element_text(size = 8, color = "grey40"))

print(g2)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

library(RColorBrewer)
options(scipen=1000000)

camdemo <- inner_join(transactions, campaigns, by = "household_id")
camdemo1 <- inner_join(camdemo, campaign_descriptions, by = "campaign_id")

data2 <- camdemo1 %>%
  group_by(sales_value, week, campaign_type) %>%
  summarise(total_sales_value = sum(sales_value)) %>%
  arrange(desc(total_sales_value)) %>%
  filter(week <= 30)
## `summarise()` has grouped output by 'sales_value', 'week'. You can override
## using the `.groups` argument.
g3 <- ggplot(data2, aes(x = week, y = total_sales_value, fill = campaign_type)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.7) +
  scale_fill_manual(values = c("pink1", "darkseagreen3", "skyblue2")) +
  labs(title = "Sales Generated by Campaign Type A, B, and C from Week 1 to Week 30",
       x = "Week",
       y = "Sales") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
g3 <- g3 + labs(subtitle = "This graph shows the total sales value generated by campaign type A, B, and C from week 1 to week 30. \nCampaign type A brought the largest sales, noticeably in week 17.")
g3 <- g3 + theme(plot.subtitle = element_text(size = 8, color = "darkseagreen4"))

print(g3)