###library(dplyr)
###library(ggplot2)
###library(readxl)
###library(tidyverse)
###library(plotly)
###library(viridis)
###library(colorspace)
###library(ggthemes)

Big Idea

We plan to earn customer loyalty and increase revenue through machine-readable marketing data, which allows us to create a user behavior profile and more accurately predict customer behavior to use in forecasting revenue.

The Data

This data came from: https://www.kaggle.com/imakash3011/customer-personality-analysis

marketing_campaign_original <-read_excel("Mark_Campaign_Clean.xlsx")

Convert data to tibble

marketing_campaign <- as_tibble(marketing_campaign_original)

Dropping levels

## Remove Absurd, Alone and YOLO, Minor, Silent

marketing_campaign <- marketing_campaign %>%
  filter(Marital_Status != "Absurd", Marital_Status !=  "Alone", Marital_Status !=  "YOLO", Age_Grp != "Minor", Age_Grp != "Silent")%>%
  droplevels()

Income by Age Group

gpl<- marketing_campaign %>%
    filter(Income <= 150000)%>%

  ggplot(aes(Age, Income), group_by= Age, colour= Age)+
    geom_col()+
  scale_y_continuous(labels = scales::comma)+
  
  theme_economist()+

  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor.x = element_blank(),
    legend.position = "bottom"
    
    )

ggplotly(gpl)

What products do people buy the most by generation?

p10 <- marketing_campaign %>%
  
select(c(Age_Grp, "MntWines", "MntFruits", "MntMeatProducts", "MntFishProducts", "MntSweetProducts", "MntGoldProds")) %>%
  mutate(Wines = as.integer(MntWines),
         Fruits= as.integer(MntFruits),
         Meat = as.integer(MntMeatProducts),
         Fish = as.integer(MntFishProducts),
         Sweet = as.integer(MntSweetProducts),
         Gold = as.integer(MntGoldProds))%>%
  select(c(Age_Grp, Wines, Fruits, Meat, Fish, Sweet, Gold))%>%
  pivot_longer(-(Age_Grp), names_to= "Products", values_to= "Value")%>%
  
  ggplot(aes(x=Age_Grp, y=Value, fill=Age_Grp))+
  geom_boxplot()+
  
  theme(
       axis.text.x = element_text(hjust = 0.8)

  ) +
    facet_wrap(.~Products, scales = "free_y")


p10

What channels do people use the most to buy items?

marketing_campaign %>%
  select(c(Age_Grp,"NumWebPurchases", "NumCatalogPurchases", "NumStorePurchases"))%>%
  mutate(Online = as.integer(NumWebPurchases),
         Catalog = as.integer(NumCatalogPurchases),
         Store = as.integer(NumStorePurchases))%>%
  select(c(Age_Grp, Online, Catalog, Store))%>%
  pivot_longer(-(Age_Grp), names_to = "Channels", values_to= "Value") %>%
  filter(Value < 20)%>%

  
ggplot(aes(x=Channels, y=Value, fill = Channels))+
  geom_violin(trim=FALSE)+
  geom_boxplot(width=0.1, fill="white")+
  facet_wrap(.~Age_Grp, scales = "free_y")+
  theme_wsj()+
  labs(y="")+
  theme(
    legend.position = "none")

What is the relationship between Income and Average Purchase per order and Education?

p12 <- marketing_campaign %>%
  filter(Avg_Purchase_Per_Order < 100)%>%
  filter(Income < 150000)%>%
    mutate(Avg_Purchase_Per_Order=round(Avg_Purchase_Per_Order)) %>%
    arrange((Kidhome)) %>%

  ggplot(aes(Avg_Purchase_Per_Order, Income, fill= Education))+
  
  geom_point(alpha=0.5, shape=21,  
               position = "jitter") +
  geom_density_2d_filled(alpha = 0.5) +
  geom_density_2d(size = 0.25, colour = "yellow")+

  
  facet_grid(.~Education)+
  
  theme(
          axis.text.y = element_text(size = 8), 
          axis.text.x = element_text(size= 8),
          
          plot.caption.position = "panel",
          plot.caption = element_text(hjust = 0, size = 7),
          
          strip.text.x = element_text(face = "bold", size= 10),
          
      
          panel.background = element_blank(),
          panel.border = element_blank(),
          panel.spacing = unit(1, "lines"),
          legend.title = element_blank(),
          legend.position = "none",
          panel.grid.major = element_blank(),
          panel.grid.minor.x = element_blank()
    
)
ggplotly(p12)

Do people who have children at home spend more money in wine?

ggplot(marketing_campaign, aes(as.factor(Kidhome), MntWines, color= as.factor(Kidhome)))+
      geom_jitter(aes(size= MntWines, alpha= 0.1))+
      scale_alpha(guide = 'none')+
  
      scale_size("Amount of\nMoney", range= c(1,5))+
  scale_color_viridis_d(option = "viridis", name= "Number of\nKids")+
    labs( x = "Number of Kids at Home",
        y = "Amount Spent on Wine")+
  
  theme(
          axis.line.y=element_blank(),
          axis.line.x = element_blank(),
          axis.ticks = element_blank(),
          axis.text.y = element_text(size = 6.5, ), 
          axis.text.x = element_text(size= 6.5),
          
          panel.grid.minor.y = element_blank(),
          panel.grid.major.y = element_line(size= 0.1, color= "white",
                                            linetype= "solid"),
          panel.background = element_rect(fill = "lightgrey"),
          panel.border = element_blank(),
          panel.spacing = unit(1, "lines"),
          
          strip.background= element_rect(fill= "white", linetype = "blank"),
          strip.text = element_text(color= "black", face= "bold"),
          strip.text.x = element_text(face = "bold", size= 10),
          
          legend.position = "bottom",
          legend.margin=margin(grid::unit(0,"cm")),
          legend.key.width=grid::unit(0.5,"cm"), 
          legend.key.height=grid::unit(0.5,"cm"))+
  
    facet_grid(~Age_Grp)

What about people that have teenagers at home?

 ggplot(marketing_campaign, aes(as.factor(Teenhome), MntWines, color= as.factor(Teenhome)))+
  
  geom_jitter(aes(size= MntWines, alpha= 0.1))+
  scale_alpha(guide = 'none')+

  
  scale_size("Amount of\nMoney", range= c(1,5))+
  scale_color_viridis_d(option = "viridis", name= "Number of\nTeens")+


   labs( x = "Number of Teenagers at Home",
        y = "Amount Spent on Wine")+
  
  theme(
          axis.line.y=element_blank(),
          axis.line.x = element_blank(),
          axis.ticks = element_blank(),
          axis.text.y = element_text(size = 6.5, ), 
          axis.text.x = element_text(size= 6.5),
          
          panel.grid.minor.y = element_blank(),
          panel.grid.major.y = element_line(size= 0.1, color= "white",
                                            linetype= "solid"),
          panel.background = element_rect(fill = "lightgrey"),
          panel.border = element_blank(),
          panel.spacing = unit(1, "lines"),
          
          strip.background= element_rect(fill= "white", linetype = "blank"),
          strip.text = element_text(color= "black", face= "bold"),
          strip.text.x = element_text(face = "bold", size= 10),
          
          legend.position = "bottom",
          legend.margin=margin(grid::unit(0,"cm")),
          legend.key.width=grid::unit(0.5,"cm"), 
          legend.key.height=grid::unit(0.5,"cm")

          )+
  
  facet_grid(~Age_Grp)