library(readxl)
## Warning: package 'readxl' was built under R version 4.4.3
library(DT)
## Warning: package 'DT' was built under R version 4.4.3
Supermarket_Transactions <- read_excel("D:/tai/Supermarket Transactions.xlsx")
## New names:
## • `` -> `...1`
datatable(Supermarket_Transactions)
## Warning in instance$preRenderHook(instance): It seems your data is too big for
## client-side DataTables. You may consider server-side processing:
## https://rstudio.github.io/DT/server.html
names (Supermarket_Transactions)
## [1] "...1" "PurchaseDate" "CustomerID"
## [4] "Gender" "MaritalStatus" "Homeowner"
## [7] "Children" "AnnualIncome" "City"
## [10] "StateorProvince" "Country" "ProductFamily"
## [13] "ProductDepartment" "ProductCategory" "UnitsSold"
## [16] "Revenue"
dt <- c("Gender", "Homeowner", "MaritalStatus", "City", "StateorProvince", "Country", "ProductFamily", "ProductDepartment")
df_dt <- Supermarket_Transactions[,dt]
table(df_dt$Gender)/sum(nrow(df_dt))
##
## F M
## 0.5099936 0.4900064
Vậy trong bộ dữ liệu có 50.9993598% nữ và 49.0006402% nam
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
# Đếm số lượng đơn hàng theo quốc gia, tính tỷ lệ %
df_p <- df_dt %>%
count(Gender) %>%
mutate(percent = n / sum(n),
label = scales::percent(percent, accuracy = 0.1))
# Vẽ pie chart
ggplot(df_p, aes(x = "", y = percent, fill = Gender)) +
geom_col(width = 1, color = "black") + # tạo cột cho pie chart
coord_polar(theta = "y") + # chuyển sang hình tròn
geom_text(aes(label = label),
position = position_stack(vjust = 0.5), size = 3, color = "black") + # label % ở giữa mỗi lát
labs(title = "Tỷ lệ % số đơn hàng theo quốc gia") +
scale_fill_brewer(palette = "Set3") # màu sắc dễ nhìn
table(df_dt$ProductDepartment)/sum(nrow(df_dt))
##
## Alcoholic Beverages Baked Goods Baking Goods Beverages
## 0.025321858 0.030229746 0.076250089 0.048367594
## Breakfast Foods Canned Foods Canned Products Carousel
## 0.013372217 0.069492852 0.007753041 0.004196600
## Checkout Dairy Deli Eggs
## 0.005832563 0.064229319 0.049719041 0.014083505
## Frozen Foods Health and Hygiene Household Meat
## 0.098300021 0.063518031 0.101002916 0.006330464
## Periodicals Produce Seafood Snack Foods
## 0.014368020 0.141830856 0.007255139 0.113806103
## Snacks Starchy Foods
## 0.025037343 0.019702682
Loại các sản phẩm Meat chiếm tỷ lệ nhỏ nhất với 0.6330464%. Ngoài ra còn nhiều món hàng khác nhau.
# Đếm số lượng và tính tỷ lệ phần trăm trên tổng số
df_t <- df_dt %>%
count(ProductDepartment) %>%
mutate(percent = n / sum(n))
# Vẽ biểu đồ
ggplot(df_t, aes(x = ProductDepartment, y = n, fill = ProductDepartment)) +
geom_col(position = position_dodge(width = 0.9)) +
geom_text(aes(label = scales::percent(percent, accuracy = 0.1)),
position = position_dodge(width = 0.9),
vjust = -0.5, size = 2.5) +
labs(
title = "Lượng mua hàng trên mỗi hàng hóa",
x = "Hàng hóa",
y = "Lượng mua hàng") +
theme(
axis.text.x = element_text(angle = 60, hjust = 1),
legend.position = "none" # Xóa đề mục màu
)
table(df_dt$ProductFamily, df_dt$Homeowner)/sum(nrow(df_dt))
##
## N Y
## Drink 0.03584892 0.05306210
## Food 0.28615122 0.43601963
## Non-Consumable 0.07738815 0.11152998
Dựa trên bảng tần số giữa nhóm sản phẩm (ProductFamily)
và tình trạng sở hữu nhà (Homeowner), ta nhận thấy rằng
thực phẩm là nhóm hàng chiếm tỷ lệ cao ở cả hai nhóm:
=> Điều này cho thấy nhu cầu về thực phẩm là rất lớn, bất kể tình trạng sở hữu nhà của người tiêu dùng.