library(trelliscopejs)
library(tidyverse)
library(ggplot2)
library(dslabs)
library(dplyr)
library(plotly)
library(scales)

Introduction

The dataset concerns Walmart’s 45-store sales between 2010 and 2012. Walmart runs several promotional markdown events throughout the year, including the Super Bowl, Labour Day, Thanksgiving, and Christmas. Also, we can investigate the relationship between sales using different indexes, including temperature, CPI index, Unemployment Index, etc. We will find out which index has a strong relationship with sales and which does not.

warlmart<-read.csv("C://Users/ROG/Desktop/Walmart_dataset.csv")

ggplot graph about the Walmart dataset.

First, we are going to find out the sales situation of different stores. Are these stores selling differently or at the same level? How much money could one store sell differently than another?

warlmart$Store <- as.factor(warlmart$Store)
ggplot(warlmart,aes(Store, Weekly_Sales,color = Store))+
  geom_boxplot()+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5))+
  labs(title = "Relationship between Store and Weekly Sales", x= "Store Number", y = "Weekly Sales(dollar)")+
  scale_y_continuous(labels = scales::label_number(scale = 1e-6, suffix = "M"),breaks = scales::pretty_breaks(n = 10))

As we see, the weekly sales of different stores are different. Store numbers 4 and 14,20 are about 2 million dollars a week, with some outliers showing these stores’ weekly sales are above 3 million sometimes. But in some other stores, like numbers 20,33,38,44, the weekly sales are below 0.5 million dollars without any exceptional sales amount. It shows that the weekly sales are different when the store’s location changes. It may be because of the economy of the local area, political reasons, or other reasons.

Base R graph about the relationship between Temperature and Weekly Sales

Next, we will investigate the relationship between Temperature, Fuel Price, CPI index, Unemployment rate, and weekly sales. Once the temperature goes higher, will it influence the weekly sales? Will weekly sales go higher or lower?

warlmart$Weekly_Sales_millions <- warlmart$Weekly_Sales/1e6

plot(warlmart$Temperature,warlmart$Weekly_Sales_millions,
     lwd = 1.6,
     xlab = "Temperature",
     ylab = "Weekly Sales(Million)",
     main = "Relationship between Temperature and Weekly Sales",
     col = rgb(0, 0, 1, 0.2), 
     pch = 16)

axis(1,at =c(10,30,50,70,90),labels = FALSE,tck = -.025)
axis(2,at =c(0.5,1.5,2.5,3.5),labels = FALSE,tck = -.025)

Upon analyzing the graph, a notable pattern emerges: when the temperature is low, the weekly sales tend to be correspondingly low. However, intriguingly, there are outliers between 20F - 50F, where the weekly sales spike significantly. At around 60F, the weekly sales stabilize, with no further outliers. These outliers, particularly in the 20F-50F range, warrant further investigation. However, as we’ve discussed, weekly sales vary significantly across different stores. Some stores may be located in cold areas, while others are not. This complexity makes it challenging to determine if the weekly sales are influenced by the store’s location or the changing temperature. Therefore, we propose to zoom in on one store, conducting a comprehensive analysis of how the weekly sales change when other indexes fluctuate. This detailed analysis is crucial for understanding the true relationship between temperature and sales.

plot_ly graph shows the relationship between temperature and weekly sales in store 4

Next, we will zoom into one store to see how the weekly sales changed when the temperature changed. I picked store 4 because it has extensive weekly sales and some outliers. It is easy to see the weekly changes when some indexes change.

store4_warlmart <- warlmart %>%
  filter(Store == 4)
plot_ly(data = store4_warlmart, x = ~Temperature,y = ~Weekly_Sales,text = ~paste("Date: ", Date, "\nTemperature:", Temperature, "\nWeekly Sales: ", Weekly_Sales)) %>%
  layout(xaxis = list(title = "Temperature(F)"),
         yaxis = list(title = "Weekly Sales(dollars)"),
         title = "The Relation of Temperature and Weekly Sales of Store4")

It is interesting to see that when the temperature increases, the weekly sales seem to decrease. The main reason is that there are some huge weekly sales between 30F and 50F. If we move our cursor on these points, we find out the dates of these points are between November and December, but no one is in January.

plot_ly graph shows the relationship between Fuel Price and weekly sales in store 4

Next, we will see how the weekly sales changed when the Fuel_Price changed for that week. Let’s see, when the fuel price increases, are the weekly sales increasing or decreasing?

plot_ly(data = store4_warlmart, x = ~Fuel_Price,y = ~Weekly_Sales,text = ~paste("Date: ", Date, "\nTemperature:", Fuel_Price, "\nWeekly Sales: ", Weekly_Sales)) %>%
  layout(xaxis = list(title = "Fuel_Price(Dollar/Gallon)"),
         yaxis = list(title = "Weekly Sales(Dollars)"),
         title = "The Relation of Fuel Price and Weekly Sales of Store4")

When the fuel price increases, it is hard to tell whether weekly sales are increasing or decreasing. But if we remove the outliers, which have weekly sales of over 2.5 million, it seems like weekly sales are increasing. Does this mean people prefer to go shopping when the fuel price is higher? Let’s try to find out more clues.

plot_ly graph shows the relationship between CPI and weekly sales in store 4

The Consumer Price Index(CPI) measures the average price of a basket of consumer goods and services. How did weekly sales change when the CPI changed for that week?

plot_ly(data = store4_warlmart, x = ~CPI,y = ~Weekly_Sales,text = ~paste("Date: ", Date, "\nCPI:", CPI, "\nWeekly Sales: ", Weekly_Sales)) %>%
  layout(xaxis = list(title = "CPI"),
         yaxis = list(title = "Weekly Sales(Dollars)"),
         title = "The Relation of CPI and Weekly Sales of Store4")

When the CPI increased, it looked like weekly sales were also increasing. This is because the ICP increased, the unit price of the good was higher, and people purchased more money on the same products. That also explained why the fuel price went higher, and weekly sales increased.

plot_ly graph shows the relationship between Unemployment and weekly sales in store 4

How did the weekly sales change when the Unemployment rate was higher?

plot_ly(data = store4_warlmart, x = ~Unemployment,y = ~Weekly_Sales,text = ~paste("Date: ", Date, "\nUnemployment:", Unemployment, "\nWeekly Sales: ", Weekly_Sales)) %>%
  layout(xaxis = list(title = "Unemployment rate"),
         yaxis = list(title = "Weekly_Sales(Dollars)"),
         title = "The Relation of Unemployment and Weekly Sales of Store4")

The weekly sales decreased as the unemployment rate increased. When the unemployment rate is below 6%,most weeks’ sales are above 2 million dollars in store 4. When the unemployment rate is above 7%, most weeks’ sales are below 2 million dollars.

trelliscope graph about the weekly sales in 2010-2012 of different stoles.

Finally, we are going to zoom into each store to see how the weekly sales changed between 2010 and 2012 and obtain more detailed information.

warlmart <- warlmart %>%
  mutate(Date = as.Date(Date, format = "%d-%m-%Y"))
ggplot(warlmart, aes(x = Date, y = Weekly_Sales)) +
  geom_line() +
  geom_vline(aes(xintercept = as.Date("2010-11-26"), linetype = "Thanksgiving"), color = "purple") + 
  geom_vline(aes(xintercept = as.Date("2011-11-25"), linetype = "Thanksgiving"), color = "purple")+
  geom_vline(aes(xintercept = as.Date("2010-12-24"), linetype = "Christmas"), color = "red") + 
  geom_vline(aes(xintercept = as.Date("2011-12-23"), linetype = "Christmas"), color = "red")+
  scale_linetype_manual(name = "", values = c("Thanksgiving" = "dashed","Christmas" = "dashed"), labels = c("Thanksgiving" = "Thanksgiving","Christmas" = "Christmas"),breaks = c("Thanksgiving","Christmas")) +
  scale_x_date(date_breaks = "2 months", date_labels = "%Y-%m")+
  scale_y_continuous(labels = label_number(scale = 1e-6, suffix = "M")) +
  labs(title = "Sales Trends by Store",
       y = "Weekly Sales(Million)")+
  theme(legend.position = "bottom",axis.text.x = element_text(angle = 45, hjust = 1),
        plot.title = element_text(hjust = 0.5)) +
  facet_trelliscope(~ Store,
                    name = "Sales by Store Over Time",
                    desc = "Weekly sales from 2010 to 2012",
                    nrow = 2, ncol = 3,
                    self_contained = TRUE,
                    scales = "free",
                    path = 'trelliscope_output'
  )

After we zoomed into each store, we discovered that most stores’ weekly sales increased significantly between November and December. The reason is that People purchase a lot of things before Thanksgiving (November 28th) and Christmas(December 25th). That also explains why there are some outliers when the temperature goes down in November and December but no outliers in January. However, some stores have no big weekly sales in Store 33, 36, 38, 42, or 43. The weekly sales of these stores are all above 0.8 million dollars or lower. The reason may be that these stores are not in the USA or Europe. They don’t celebrate Christmas or Thanksgiving, and when the currency converts to the US dollar, the sales number becomes pretty low.