E-commerce has reshaped how businesses operate and engage with customers. With the rapid growth of online shopping, understanding and analyzing e-commerce sales data is important for businesses aiming to remain competitive. This article will guide you through the process of analyzing e-commerce sales data using R.
Project Overview
In this project, we will analyze e-commerce sales data to extract actionable insights that can help businesses optimize their operations and strategies. We will focus on the following key areas:
- Total Orders and Revenue by Day of the Week: We will examine which days generate the most orders and revenue to help businesses optimize marketing and inventory efforts on peak days.
- Average Order Value Over Time: We will track how the average order value changes over time, identifying shifts in customer spending behavior and helping businesses adjust their strategies.
- Order Distribution by Time of Day: We will analyze order trends based on the time of day to identify peak shopping hours and adjust operations accordingly.
- Weekly Order and Revenue Trends: We will explore weekly patterns in orders and revenue to identify opportunities for targeted marketing campaigns and improve resource allocation.
1. Loading the Libraries and Dataset
We will need to load the necessary libraries ggplot2 ,dplyr and tidyr for analysis. We will be using the E-commerce Sales Dataset for this project which can be downloaded from here.
install.packages(c("ggplot2","tidyr","dplyr"))
library(ggplot2)
library(tidyr)
library(dplyr)
df <-read.csv("/content/Sales.csv")
head(df)
Output:

2. Total Orders and Revenue by Day of the Week
We are aggregating the data by day of the week to visualize the total number of orders and revenue generated on each day. Understanding which days generate the most orders and revenue can help businesses optimize marketing efforts and inventory management.
install.packages("gridExtra")
library(gridExtra)
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 <- 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() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
plot_revenue <- 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() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
grid.arrange(plot_orders, plot_revenue, ncol = 2)
Output:

- We identified which days of the week generate the highest number of orders. This helps businesses target their marketing efforts and optimize inventory on peak shopping days.
- The second chart shows the days contributing the highest revenue, providing insights into purchasing patterns and helping businesses plan promotions or discounts on specific days.
3. Average Order Value Over Time
We are calculating the average order value by month and plotting the trend over time to identify any fluctuations in customer spending. Tracking how the average order value changes over time helps businesses identify shifts in customer spending behavior.
df <- df %>%
mutate(LATEST_ORDER_DATE = as.Date(LATEST_ORDER_DATE))
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))
orders_by_month <- orders_by_month %>%
mutate(month = as.Date(paste0(month, "-01")))
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:

This line chart helps track how the average order value evolves over time. We observed that fluctuations in the average order value can reflect seasonal changes, customer preferences, or the impact of promotions and discounts.
4. Order and Revenue Distribution by Time of Day
We are aggregating the orders and revenue by time of day (morning, afternoon, evening, night) to visualize the patterns of order placement throughout the day. Analyzing when most orders are placed can assist in optimizing operational efficiency and marketing strategies.
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_d <- 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()+
theme(axis.text.x = element_text(angle = 90, hjust = 1))
plot_revenue_d <- 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() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
grid.arrange(plot_orders_d, plot_revenue_d, ncol = 2)

- We identified peak shopping hours, helping businesses optimize operations and target marketing during high-traffic periods.
- The second chart indicates which time periods generate the most revenue, allowing businesses to focus their efforts during high-revenue windows.
5. Weekly Order and Revenue Trends
We are aggregating the total orders and revenue by week and visualizing them using donut charts to show the distribution over the weeks. This helps identify patterns and opportunities for targeted marketing campaigns.
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))
# 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")
}
grid.arrange(make_donut_chart(week_orders, "Total_Orders", "Weekly Orders") , make_donut_chart(week_revenue, "Total_Revenue", "Weekly Revenue" ))
Output:

The donut charts visualize the distribution of orders and revenue across weeks. We were able to identify which weeks had the highest sales, allowing businesses to plan for future campaigns or allocate resources accordingly.
Conclusion
By visualizing E-commerce sales data in R, we identified key trends and patterns:
- We identified peak days for orders and revenue, helping businesses optimize marketing efforts and inventory management.
- We observed fluctuations in customer spending, providing insights into seasonal changes or shifts in purchasing behavior.
- We identified peak shopping hours and high-revenue periods, assisting in operational planning and marketing timing.
- We discovered high-activity weeks, guiding businesses in planning promotional campaigns and resource allocation.
These visualizations help businesses make informed decisions, optimize operations, and drive growth through strategic insights.