library(ggplot2)
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(stringr)
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
transactions <- transactions_sample

# Sales by Department with Age Restriction
products %>% 
  inner_join(transactions, by = "product_id") %>% 
  select(household_id, department, sales_value) %>% 
  inner_join(demographics, by = "household_id") %>% 
  filter(str_detect(age, "65+")) %>% 
  group_by(department) %>% 
  summarize(total_sales_value = sum(sales_value)) %>% 
  filter(total_sales_value > 100) %>% 
  ggplot(aes(x = total_sales_value, y = department)) +
  geom_col() +
  scale_x_continuous("Total Sales") +
  scale_y_discrete("Department") +
  ggtitle("Total Sales by Department For Senior Citizens (65+)")

# Sales by Age Chart
demographics %>% 
  inner_join(transactions, by = "household_id") %>% 
  select(household_id, age, retail_disc, sales_value) %>% 
  group_by(age) %>% 
  summarize(total_sales_value = sum(sales_value)) %>% 
  ggplot(aes(x = age, y = total_sales_value)) +
  geom_col() +
  scale_x_discrete("Age") +
  scale_y_continuous("Total Sales") +
  ggtitle("Total Sales by Age")

# Sales from Coupon Purchases Grouped by Income Level
coupons %>% 
  inner_join(transactions, by = "product_id") %>% 
  group_by(household_id) %>% 
  summarize(total_sales_value = sum(sales_value)) %>% 
  inner_join(demographics, by = "household_id") %>% 
  group_by(income) %>% 
  summarize(total_sales_value = sum(total_sales_value)) %>% 
  ggplot(aes(x = income, y = total_sales_value)) +
  geom_col() +
  scale_x_discrete("Income Level") +
  scale_y_continuous("Total Sales") +
  ggtitle("Total Sales from Coupon Purchases by Income Level")
## Warning in inner_join(., transactions, by = "product_id"): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 1 of `x` matches multiple rows in `y`.
## ℹ Row 18806 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.