library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(stringr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(forcats)
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble 3.1.8     ✔ readr  2.1.2
## ✔ tidyr  1.2.1     ✔ purrr  0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
transactions <- get_transactions()
promotions <- get_promotions()
demo <- demographics
coupons<-coupon_redemptions
transactions_sample <- transactions_sample 
df <- transactions %>%
  inner_join(products, by = 'product_id') %>%
  inner_join(demographics, by = 'household_id') %>%
  group_by(product_category, age) %>%
  summarise(sales = sum(sales_value)) %>%
  arrange(desc(sales)) %>%
  head(10)
## `summarise()` has grouped output by 'product_category'. You can override using
## the `.groups` argument.
df %>%
  ggplot(aes(x = reorder(product_category, sales, FUN = sum), 
             y = sales, 
             fill = age
             )
         ) +
    geom_col() +
    coord_flip() +
    labs(
      title = 'Plot 1 : Top 10 Product Categories',
      y = 'Total Sales ',
      x = 'Product Category',
      subtitle = 
'Top 10 product category by age'
    ) +
    guides(fill = guide_legend(title = "Age")) +
    theme(plot.title = element_text(face = "bold", size = 20), 
        plot.subtitle = element_text(size = 9),
        axis.text = element_text(size = 8)
        )

products %>%
  filter(str_detect(brand, regex("national", ignore_case = TRUE))) %>%
  inner_join(transactions, by = "product_id") %>%
  mutate(total_sales = as.numeric(sum(sales_value))) %>%
  mutate(product_cat = fct_lump(product_category, n = 7)) %>%
  filter(!is.na(product_cat)) %>%
  ggplot(aes(x = product_cat, y = total_sales)) +
  geom_col() +
  coord_flip() +
  scale_y_continuous(labels = scales::dollar) +
  theme(axis.text.x = element_text(angle = 25, hjust = 1)) +
  labs(title = "Plot 2 : National Label Product Sales",
       subtitle = "National Label Products by Total Sales per Category",
       caption = "via complete journey package",
       y = "Total Sales",
       x = "Product Category") +
  theme_minimal()

transactions_sample %>%
   inner_join(products, by = "product_id") %>%
   mutate(department = fct_lump(department, n = 20)) %>%
   group_by(department) %>%
   summarize(total_spend = sum(sales_value)) %>%
   ggplot(aes(total_spend, fct_reorder(department, total_spend))) +
   geom_point()+
labs(title = "Plot 3 : Total spend by department",
       caption = "via complete journey package",
       y = "Department",
       x = "total spend")