library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(forcats)
library(ggplot2)
library(stringr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## 
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
options(dplyr.summarise.inform = FALSE)

transactions <- get_transactions()

df2 <- campaigns %>%  inner_join(campaign_descriptions, by ="campaign_id")%>% 
      mutate(Duration = end_date - start_date)
       

ggplot(data = df2, aes(x=campaign_id, y=as.numeric(Duration), col=campaign_type)) + 
  geom_point(size=4)+
  geom_segment(aes(x=campaign_id, 
                   xend=campaign_id, 
                   y=0, 
                   yend=Duration)) + 
  labs(title="Campaign Running Duration", 
       caption="source: Complete Data Journey",
       x = "Campaign Identifier",
       y = "Campaign Duration(In Days)",
       fill = "Campaign Type") +
    scale_colour_discrete("Campaign Type")+
  theme(axis.text.x = element_text( vjust=0.5)) +
  theme_light()

options(dplyr.summarise.inform = FALSE)



df1 <- campaigns %>%
       inner_join(campaign_descriptions, by = "campaign_id") %>%
       inner_join(demographics, by = "household_id") %>%
       inner_join(transactions, by = "household_id") %>%
       group_by(household_id,income) %>%
       summarise(Total_Coupons = n_distinct(campaign_id),
                 Total_Purchase = sum(sales_value, na.rm=T) ) %>%
       arrange(desc(Total_Coupons)) 

 ggplot(data = df1, aes(x = Total_Coupons, y = Total_Purchase)) +
  geom_point(shape=3, color="blue",alpha = 1 / 3)+
  geom_smooth(method=lm,  linetype="dashed",formula = y ~ x,
              color="darkred", fill="blue")+
  labs(title="Household Spending vs # of coupons received", 
     subtitle="Each Household Spending based on # of coupons received", 
     caption="source: Complete Data Journey",
     x = "Coupon Count",
     y = "Total Sales Value") + 
     theme(axis.text.x = element_text(vjust=0.6))+
      theme_light()

 ggplot(data = df1, aes(x = Total_Coupons, y = Total_Purchase)) +
   geom_point(shape=4, color="blue",alpha = 1 / 3) +
   facet_wrap(~ income, nrow = 4) +
 labs(title="Campaign impact based on Household Income",
      caption="source: Complete Data Journey",
      x = "Total Number of Campaigns",
      y = "Total Purchase in a Year") + 
   theme(axis.text.x = element_text(vjust=0.6))  +
   theme_minimal()