library(ggplot2)  
library(completejourney)
## 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(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
df <- transactions_sample %>%
     inner_join(products) %>%
  inner_join(demographics)
## Joining with `by = join_by(product_id)`
## Joining with `by = join_by(household_id)`
df %>%
   filter(department == 'DELI') %>%
   group_by(week_of_year = week(transaction_timestamp)) %>%
   summarize(total_sale = sum(sales_value)) %>%
        ggplot(aes(x = week_of_year, 
                     y = total_sale),
                 fill = "blue",
                 width = 0.7)+
                 geom_line() +
                 ggtitle("Sales distribution of Deli products by week in a year",
                subtitle = "Which time of the year are Deli products most popular?")+
        labs(x = "Week",
             y = "Total sales amount")+
        theme(axis.title = element_text(size = 15, face = "bold"),
              axis.text = element_text(size = 10),
              plot.title = element_text(size = 20, hjust = 0.5),
              plot.subtitle = element_text(size = 15, hjust= 0.5))

df %>%
   filter(department == 'DELI') %>%
   group_by(day_of_week = wday(transaction_timestamp, label = TRUE)) %>%
   summarize(total_sale = sum(sales_value)) %>%
        ggplot(aes(x = day_of_week, 
                     y = total_sale),
                 fill = "blue",
                 width = 0.7)+
                 geom_col() +
        ggtitle("Sales distribution of Deli products by day of the week",
                subtitle = "Do Deli products sell more during weekdays or weekends?")+
        labs(x = "Day of the week",
             y = "Total sales amount")+
        theme(axis.title = element_text(size = 15, face = "bold"),
              axis.text = element_text(size = 10),
              plot.title = element_text(size = 20, hjust = 0.5),
              plot.subtitle = element_text(size = 15, hjust= 0.5))

df %>%
   filter(department == 'DELI') %>%
   group_by(hour_of_day = hour(transaction_timestamp)) %>%
   summarize(total_sale = sum(sales_value)) %>%
        ggplot(aes(x = hour_of_day, 
                     y = total_sale),
                 fill = "blue",
                 width = 0.7)+
                 geom_col() +
        ggtitle("Sales distribution by time of the day",
                subtitle = "When to stock Deli products based on buy pattern during the day?")+
        labs(x = "Time of the day",
             y = "Total sales amount")+
        theme(axis.title = element_text(size = 15, face = "bold"),
              axis.text = element_text(size = 10),
              plot.title = element_text(size = 20, hjust = 0.5),
              plot.subtitle = element_text(size = 15, hjust= 0.5))