Load Packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.1 ✔ stringr 1.5.2
## ✔ ggplot2 4.0.0 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(extrafont)
## Warning: package 'extrafont' was built under R version 4.5.2
## Registering fonts with R
Load Data
sales <- read.csv("C:/Users/rmora/Downloads/sales.csv")
Create Datetime + Hour Column
sales$datetime <- as.POSIXct(
paste(sales$Date, sales$Time),
format = "%m/%d/%Y %H:%M"
)
sales$hour <- as.numeric(format(sales$datetime, "%H"))
🔹 GGPLOT 1: Total Sales by Weekday
library(ggplot2)
library(dplyr)
sales$weekday <- factor(
weekdays(as.Date(sales$datetime)),
levels = c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
)
weekday_summary <- sales %>%
group_by(weekday) %>%
summarise(Total = sum(Total)) %>%
arrange(Total)
ggplot(weekday_summary, aes(x = Total, y = weekday, fill = weekday)) +
geom_col() +
geom_text(aes(label = scales::comma(Total)),
hjust = -0.1, size = 4, fontface = "bold") +
scale_fill_brewer(palette = "Set3") +
labs(title = "Total Sales Breakdown by Weekday",
x = "Total Sales", y = "Day of the Week") +
theme_minimal(base_size = 14) +
theme(legend.position = "none") +
xlim(0, max(weekday_summary$Total) * 1.15)

🔹 GGPLOT 2: Total Sales by Hour
hour_summary <- sales %>%
group_by(hour) %>%
summarise(Total = sum(Total)) %>%
arrange(hour)
ggplot(hour_summary, aes(x = Total, y = factor(hour), fill = hour)) +
geom_col() +
geom_text(aes(label = scales::comma(Total)),
hjust = -0.1, size = 3.5, fontface = "bold") +
scale_fill_gradientn(colors = rainbow(24)) +
labs(title = "Total Sales Breakdown by Hour",
x = "Total Sales", y = "Hour of Day") +
theme_minimal(base_size = 14) +
theme(legend.position = "none") +
xlim(0, max(hour_summary$Total) * 1.15)

🔹 GGPLOT 3: Average Rating by Hour
rating_hour <- sales %>%
group_by(hour) %>%
summarise(AverageRating = mean(Rating)) %>%
arrange(hour)
ggplot(rating_hour, aes(x = factor(hour), y = AverageRating, fill = AverageRating)) +
geom_col() +
geom_text(aes(label = round(AverageRating, 2)),
vjust = -0.3, size = 4, fontface = "bold") +
scale_fill_gradientn(colors = rainbow(7)) +
labs(title = "Average Customer Rating by Hour",
x = "Hour of Day", y = "Average Rating") +
ylim(0, max(rating_hour$AverageRating) + 0.8) +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
