For this assignment, I used the Customer Personality Analysis data set which can be found here. The data set consists of 2,240 rows and 29 columns. The variables I will be focusing on are Marital_Status, Kidhome (number of children in the customer’s home), Teenhome (number of teenagers in the customer’s home), the total number of purchases made, and when they were made.
This is the following R code for how I prepared the data:
library(ggplot2)
library(ggthemes)
library(lubridate)
library(plyr)
library(plotly)
library(RColorBrewer)
library(scales)
customers <- read.csv("marketing_campaign.csv", header = TRUE, sep = "\t")
puchase_count <- nrow(customers)
purchase_date <- dmy(customers$Dt_Customer)
year <- year(purchase_date)
day <- day(purchase_date)
customers['purchase_day'] <- day
customers['purchase_year'] <- year
customers['purchase_month'] <- month(purchase_date)
customers['total_purchases'] <- customers$NumWebPurchases+customers$NumCatalogPurchases+customers$NumDealsPurchases
customers$purchase_year <- as.factor(customers$purchase_year)
To start, I was interested in seeing if there were any months in particular where there the total amount of purchases was exceptionally low or high. In order to do so, I created a histogram by producing the following R code:
p1 <- ggplot(customers, aes(x=purchase_month)) +
geom_histogram(bins=12,color="black", fill = "lightpink") +
labs(title = "Histogram of Purchases by Month", x="Purchase Month", y = "Count of Purchases") +
scale_y_continuous(labels = comma) +
stat_bin(binwidth = 1, geom="text", color = "black", aes(label = scales::comma(..count..)),vjust = -0.5)
x_axis_labels <- 1:12
p1 <- p1 + scale_x_continuous(labels = x_axis_labels, breaks = x_axis_labels)
This is the histogram generated by the code above:
To get a better feel for customer spending habits, I next wanted to examine how and if education level affects the amount of purchases a customer may make.
p2 <- ggplot(customers, aes(x=reorder(Education, total_purchases, sum), y = total_purchases, fill = purchase_year)) +
geom_bar(stat="identity") +
coord_flip()+
labs(title = "Amount of Purchases by Customer Education Level", x = "", fill = "Year") +
theme_hc() +
theme(plot.title = element_text(hjust = 0.5))+
scale_fill_brewer(palette= "Spectral", guide = guide_legend(reverse = TRUE))
This is the plot generated by the code above:
Next, I wanted to see how marital status contributed to a customer’s purchasing habits.
p3 <- plot_ly(customers, labels = ~Marital_Status, values = ~total_purchases) %>%
add_pie(hole=0.6) %>%
layout(title = "Total Purchases by Marital Status") %>%
layout(annotations=list(text=paste0("Total Purchase Count: \n",
scales::comma(sum(customers$total_purchases))),
"showarrow"= F))
Here is the pie chart generated by the code above:
Finally, I wanted to see if customers with children tended to make more puchases than those with less or no children.
customers['total_children'] <- customers$Kidhome + customers$Teenhome
p4 <- ggplot(customers, aes(x=reorder(total_children, total_purchases, sum), y = total_purchases, fill = purchase_year)) +
geom_bar(stat="identity") +
coord_flip()+
labs(title = "Total Purchases by amount of Children in Customer Household by Year ", x = "", fill = "Year") +
theme_hc() +
theme(plot.title = element_text(hjust = 0.5))+
scale_fill_brewer(palette= "PuBuGn", guide = guide_legend(reverse = TRUE))
Here is the graph produced by the R code above:
Based on graphs three and four, we see a common denominator– 2013 was the year where most sales were made. In terms of the least purchases made, graph one shows us that July, on average, was the worst. This may indicate the natural decline in retail sales that occurs generally in July, or it can indicate something more serious. Regardless, it is worth looking into. Luckily, the decline in sales for the month of July does not appear to cause a downward trend, so it is not too alarming. Graph two shows us that those who have attained the education level “Graduation” tend to make the most amount of purchases, followed by PhD, Masters, 2n Cycle, and finally Basic.Graph three shows us that those who are married make the most purchases, followed by those who consider their marital status as “together.” Finally, graph four shows us that those who have only one child make the most purchases, followed by those who have none. It is interesting that those who have three children made contributed the least to total amount of purchases. It could be worthwhile to try and market towards bigger families, as they tend to need to purchase more items with a higher level of frequency.