library(completejourney)
## Warning: package 'completejourney' was built under R version 4.2.3
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
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(stringr)

Create campaign_data dataset

campaign_data <- full_join(campaigns, campaign_descriptions, by = "campaign_id")

campaign_data <- campaign_data %>%
  mutate(campaign_duration = difftime(end_date, start_date, units = "days")) %>%
  group_by(campaign_type) %>%
  summarise(avg_duration = mean(campaign_duration, na.rm = TRUE))

campaign_data
## # A tibble: 3 × 2
##   campaign_type avg_duration 
##   <ord>         <drtn>       
## 1 Type A        49.68138 days
## 2 Type B        35.31841 days
## 3 Type C        66.00174 days

Create bar chart

ggplot(data = campaign_data, aes(x = campaign_type, y = avg_duration, fill = campaign_type)) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  labs(title = "Average Campaign Duration by Type", x = "Campaign Type", y = "Average Duration")
## Don't know how to automatically pick scale for object of type <difftime>.
## Defaulting to continuous.

products
## # A tibble: 92,331 × 7
##    product_id manufacturer_id department    brand    product_c…¹ produ…² packa…³
##    <chr>      <chr>           <chr>         <fct>    <chr>       <chr>   <chr>  
##  1 25671      2               GROCERY       National FRZN ICE    ICE - … 22 LB  
##  2 26081      2               MISCELLANEOUS National <NA>        <NA>    <NA>   
##  3 26093      69              PASTRY        Private  BREAD       BREAD:… <NA>   
##  4 26190      69              GROCERY       Private  FRUIT - SH… APPLE … 50 OZ  
##  5 26355      69              GROCERY       Private  COOKIES/CO… SPECIA… 14 OZ  
##  6 26426      69              GROCERY       Private  SPICES & E… SPICES… 2.5 OZ 
##  7 26540      69              GROCERY       Private  COOKIES/CO… TRAY P… 16 OZ  
##  8 26601      69              DRUG GM       Private  VITAMINS    VITAMI… 300 CT…
##  9 26636      69              PASTRY        Private  BREAKFAST … SW GDS… <NA>   
## 10 26691      16              GROCERY       Private  PNT BTR/JE… HONEY   12 OZ  
## # … with 92,321 more rows, and abbreviated variable names ¹​product_category,
## #   ²​product_type, ³​package_size
transactions_sample
## # A tibble: 75,000 × 11
##    house…¹ store…² baske…³ produ…⁴ quant…⁵ sales…⁶ retai…⁷ coupo…⁸ coupo…⁹  week
##    <chr>   <chr>   <chr>   <chr>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <int>
##  1 2261    309     316252… 940996        1    3.86    0.43       0       0     5
##  2 2131    368     320531… 873902        1    1.59    0.9        0       0    10
##  3 511     316     324458… 847901        1    1       0.69       0       0    13
##  4 400     388     319322… 130949…       2   11.9     2.9        0       0     8
##  5 918     340     320746… 1085604       1    1.29    0          0       0    10
##  6 718     324     326146… 883203        1    2.5     0.49       0       0    15
##  7 868     323     320747… 9884484       1    3.49    0          0       0    10
##  8 1688    450     348504… 1028715       1    2       1.79       0       0    33
##  9 467     31782   312807… 896613        2    6.55    4.44       0       0     2
## 10 1947    32004   327441… 978497        1    3.99    0          0       0    16
## # … with 74,990 more rows, 1 more variable: transaction_timestamp <dttm>, and
## #   abbreviated variable names ¹​household_id, ²​store_id, ³​basket_id,
## #   ⁴​product_id, ⁵​quantity, ⁶​sales_value, ⁷​retail_disc, ⁸​coupon_disc,
## #   ⁹​coupon_match_disc

Create product_transactions dataset

product_transactions <- inner_join(
  transactions_sample, products, by = "product_id"
)

custom_order <- c("January", "February", "March", "April", "May", "June",
                  "July", "August", "September", "October", "November", "December")

#product_transactions
product_transactions <- product_transactions %>%
  group_by(transaction_month = months(transaction_timestamp)) %>%
  filter(str_detect(product_category, "BREAD")) %>%
  summarise(total_retail_disc = sum(retail_disc)) %>%
  arrange(desc(transaction_month))

product_transactions <- product_transactions %>%
  mutate(transaction_month = factor(transaction_month, levels = custom_order, ordered = TRUE)) %>%
  arrange(transaction_month)

Create histogram

ggplot(product_transactions, aes(x = transaction_month, y = total_retail_disc)) +
  geom_line(color = "blue") +
  geom_point(color = "red") +
  labs(title = "Total Retail Discount for BREAD Products by Month",
       x = "Month",
       y = "Total Retail Discount") +
  theme_minimal()
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?

Create coupon_data dataset

coupon_data <- inner_join(coupons, coupon_redemptions, by = "coupon_upc")

coupon_data <- coupon_data %>%
  group_by(campaign_id.x) %>%
  summarise(total_coupons = n()) %>%
  filter(total_coupons > 250000)
coupon_data
## # A tibble: 3 × 2
##   campaign_id.x total_coupons
##   <chr>                 <int>
## 1 13                   803086
## 2 18                   989217
## 3 8                    391651

Create coupon_data bar chart

ggplot(coupon_data, aes(x = campaign_id.x, y = total_coupons)) +
  geom_bar(stat = "identity", fill = "skyblue", color = "black") +
  labs(title = "Total Coupons per Popular Campaign",
       x = "Campaign ID",
       y = "Total Coupons") +
  theme_minimal()