E-commerce Sales Analysis in R

E-commerce has transformed the way businesses operate and interact with customers. With the explosion of online shopping, understanding and analyzing e-commerce sales data has become crucial for businesses aiming to stay competitive. This article delves into the essentials of e-commerce sales analysis using R Programming Language.

Introduction to E-commerce Sales Analysis

E-commerce sales analysis involves examining various data points related to online sales to extract actionable insights. These insights can help businesses understand customer behavior offering extensive libraries and data analysis tools to optimize marketing strategies, improve inventory management, and increase overall profitability. Key metrics in e-commerce sales analysis include:

Dataset Link: E-commerce Sales Analysis

Visualizing e-commerce sales data can reveal significant insights into customer behavior, sales trends, and revenue generation. Below are multiple examples of how to visualize e-commerce sales data using the dataset provided. Each example includes the R code and an explanation of what the visualization reveals.

1. Total Orders and Revenue by Day of the Week

Understanding which days generate the most orders and revenue can help optimize marketing efforts and inventory management.

R
library(ggplot2)
library(tidyr)
library(dplyr)

# Prepare data for visualization
day_orders <- df %>%
  select(MONDAY_ORDERS, TUESDAY_ORDERS, WEDNESDAY_ORDERS, THURSDAY_ORDERS,FRIDAY_ORDERS,
         SATURDAY_ORDERS, SUNDAY_ORDERS) %>%
  gather(key = "Day", value = "Orders")

day_revenue <- df %>%
  select(MONDAY_REVENUE, TUESDAY_REVENUE, WEDNESDAY_REVENUE, THURSDAY_REVENUE, 
         FRIDAY_REVENUE, SATURDAY_REVENUE, SUNDAY_REVENUE) %>%
  gather(key = "Day", value = "Revenue")

# Plot orders by day of the week
ggplot(day_orders, aes(x = Day, y = Orders, fill = Day)) +
  geom_bar(stat = "identity") +
  labs(title = "Total Orders by Day of the Week", x = "Day", y = "Total Orders") +
  theme_minimal()

# Plot revenue by day of the week
ggplot(day_revenue, aes(x = Day, y = Revenue, fill = Day)) +
  geom_bar(stat = "identity") +
  labs(title = "Total Revenue by Day of the Week", x = "Day", y = "Total Revenue") +
  theme_minimal()

Output:

E-commerce Sales Analysis in R

Total Orders by Day of the Week: This bar chart shows which days of the week have the highest number of orders, helping identify peak shopping days. This bar chart illustrates which days generate the most revenue, revealing trends in purchasing behavior.

2. Average Order Value Over Time

Visualizing how the average order value changes over time can indicate shifts in customer spending patterns.

R
# Ensure the necessary libraries are loaded
library(dplyr)
library(ggplot2)

# Convert LATEST_ORDER_DATE to Date class if it is not already
df <- df %>%
  mutate(LATEST_ORDER_DATE = as.Date(LATEST_ORDER_DATE))

# Prepare data for visualization
orders_by_month <- df %>%
  mutate(month = format(LATEST_ORDER_DATE, "%Y-%m")) %>%
  group_by(month) %>%
  summarize(avg_order_value = mean(AVERAGE_ORDER_VALUE, na.rm = TRUE))

# Convert month to Date class for proper ordering in the plot
orders_by_month <- orders_by_month %>%
  mutate(month = as.Date(paste0(month, "-01")))

# Plot average order value over time
ggplot(orders_by_month, aes(x = month, y = avg_order_value)) +
  geom_line() +
  geom_point() +
  labs(title = "Average Order Value Over Time", x = "Month",
       y = "Average Order Value") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

Output:

E-commerce Sales Analysis in R

This line chart shows the trend in average order value across different months, helping businesses understand how customer spending evolves over time.

3. Order Distribution by Time of Day

Analyzing order distribution by time of day can help optimize operational efficiency and marketing strategies.

R
# Prepare data for visualization
time_of_day_orders <- df %>%
  select(TIME_0000_0600_ORDERS, TIME_0601_1200_ORDERS, TIME_1200_1800_ORDERS, 
         TIME_1801_2359_ORDERS) %>%
  gather(key = "Time_Period", value = "Orders")

time_of_day_revenue <- df %>%
  select(TIME_0000_0600_REVENUE, TIME_0601_1200_REVENUE, TIME_1200_1800_REVENUE, 
         TIME_1801_2359_REVENUE) %>%
  gather(key = "Time_Period", value = "Revenue")

# Plot orders by time of day
ggplot(time_of_day_orders, aes(x = Time_Period, y = Orders, fill = Time_Period)) +
  geom_bar(stat = "identity") +
  labs(title = "Order Distribution by Time of Day", x = "Time Period", y = "Orders") +
  theme_minimal()

# Plot revenue by time of day
ggplot(time_of_day_revenue, aes(x = Time_Period, y = Revenue, fill = Time_Period)) +
  geom_bar(stat = "identity") +
  labs(title = "Revenue Distribution by Time of Day",
       x = "Time Period", y = "Revenue") +
  theme_minimal()

Output:

E-commerce Sales Analysis in R

This bar chart shows when most orders are placed, helping businesses understand peak shopping hours. This bar chart illustrates revenue generated during different times of the day, providing insights into high-revenue periods.

4. Weekly Order and Revenue Trends

Visualizing orders and revenue trends across weeks can highlight patterns and potential opportunities for marketing campaigns.

R
# Load necessary libraries
library(dplyr)
library(tidyr)
library(ggplot2)

# Prepare data for visualization
week_orders <- df %>%
  select(WEEK1_DAY01_DAY07_ORDERS, WEEK2_DAY08_DAY15_ORDERS, WEEK3_DAY16_DAY23_ORDERS, 
         WEEK4_DAY24_DAY31_ORDERS) %>%
  gather(key = "Week", value = "Orders") %>%
  group_by(Week) %>%
  summarize(Total_Orders = sum(Orders))

week_revenue <- df %>%
  select(WEEK1_DAY01_DAY07_REVENUE,WEEK2_DAY08_DAY15_REVENUE,WEEK3_DAY16_DAY23_REVENUE, 
         WEEK4_DAY24_DAY31_REVENUE) %>%
  gather(key = "Week", value = "Revenue") %>%
  group_by(Week) %>%
  summarize(Total_Revenue = sum(Revenue))

# Create a function to make donut charts
make_donut_chart <- function(data, value_col, title) {
  data <- data %>%
    mutate(fraction = !!sym(value_col) / sum(!!sym(value_col)),
           ymax = cumsum(fraction),
           ymin = c(0, head(ymax, n = -1)))
  
  ggplot(data, aes(ymax = ymax, ymin = ymin, xmax = 4, xmin = 3, fill = Week)) +
    geom_rect() +
    coord_polar(theta = "y") +
    xlim(c(2, 4)) +
    theme_void() +
    labs(title = title) +
    theme(legend.position = "right")
}

# Plot donut chart for weekly orders
make_donut_chart(week_orders, "Total_Orders", "Weekly Orders")

# Plot donut chart for weekly revenue
make_donut_chart(week_revenue, "Total_Revenue", "Weekly Revenue")

Output:

E-commerce Sales Analysis in R

We Define a function make_donut_chart to create a donut chart. This function takes the data, the column name containing values to be plotted, and the chart title as arguments.

Within the function:

  • Calculate the fraction of each segment.
  • Compute the cumulative sum for ymax and create ymin for each segment.
  • Use geom_rect to create the segments and coord_polar to transform the chart into a donut shape.
  • Customize the plot appearance with theme_void for a clean look and adjust legend position.

Call the make_donut_chart function twice: once for week_orders and once for week_revenue, passing the appropriate data and value column. This will create two donut charts, one for weekly orders and another for weekly revenue.

Conclusion

Visualizing e-commerce sales data using R provides valuable insights that can drive strategic decision-making. The examples provided demonstrate how to analyze various aspects of e-commerce performance, from daily and weekly trends to time-of-day patterns. By leveraging these visualizations, businesses can optimize their operations, enhance customer experience, and ultimately drive growth.