Data ini merupakan data penjualan sepeda yang ada di berbagai Negara. Kita akan menganalisis penjualan yang dilakukan oleh Toko Sepeda. Terutama dari keuntungan, dan produk apa saja yang harus ditingkatkan.
Data diambil dari kaggle: https://www.kaggle.com/code/sadiqshah/bike-store-sales-in-europe/data
Sebelum kita memulai analisis data mari kita masukkan library yang diperlukan
library(tidyverse)## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.7 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggpubr)
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(glue)
library(plotly)##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(lubridate)##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr)sales = read.csv("data_input/Sales.csv")
head(sales, 3)Kita harus mengecek tipe data terlebih dahulu apakah tipe datanya sudah sesuai atau belum
glimpse(sales)## Rows: 113,036
## Columns: 18
## $ Date <chr> "2013-11-26", "2015-11-26", "2014-03-23", "2016-03-23…
## $ Day <int> 26, 26, 23, 23, 15, 15, 22, 22, 22, 22, 30, 30, 15, 1…
## $ Month <chr> "November", "November", "March", "March", "May", "May…
## $ Year <int> 2013, 2015, 2014, 2016, 2014, 2016, 2014, 2016, 2014,…
## $ Customer_Age <int> 19, 19, 49, 49, 47, 47, 47, 47, 35, 35, 32, 32, 34, 3…
## $ Age_Group <chr> "Youth (<25)", "Youth (<25)", "Adults (35-64)", "Adul…
## $ Customer_Gender <chr> "M", "M", "M", "M", "F", "F", "F", "F", "M", "M", "F"…
## $ Country <chr> "Canada", "Canada", "Australia", "Australia", "Austra…
## $ State <chr> "British Columbia", "British Columbia", "New South Wa…
## $ Product_Category <chr> "Accessories", "Accessories", "Accessories", "Accesso…
## $ Sub_Category <chr> "Bike Racks", "Bike Racks", "Bike Racks", "Bike Racks…
## $ Product <chr> "Hitch Rack - 4-Bike", "Hitch Rack - 4-Bike", "Hitch …
## $ Order_Quantity <int> 8, 8, 23, 20, 4, 5, 4, 2, 22, 21, 8, 8, 7, 7, 5, 7, 2…
## $ Unit_Cost <int> 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 4…
## $ Unit_Price <int> 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120…
## $ Profit <int> 590, 590, 1366, 1188, 238, 297, 199, 100, 1096, 1046,…
## $ Cost <int> 360, 360, 1035, 900, 180, 225, 180, 90, 990, 945, 360…
## $ Revenue <int> 950, 950, 2401, 2088, 418, 522, 379, 190, 2086, 1991,…
Kita lihat dari tipe data diatas. terdapat tipe-tipe data yang harus kita ubah. sebagai contoh yang perlu kita ubah adalah :
Oleh karena itu kita perlu ubah dengan cara menggunakan fungsi as.factor dan menggunakan library dpylr dengan menggunakan fungsi mutate
sales_clean <- sales %>%
mutate(
Age_Group = as.factor(Age_Group),
Customer_Gender = as.factor(Customer_Gender),
Country = as.factor(Country),
State = as.factor(State),
Product_Category = as.factor(Product_Category),
Sub_Category = as.factor(Sub_Category),
Product = as.factor(Product),
Date = ymd(Date)
)ayo kita mengecek kembali data yang telah kita ubah tipe datanya
glimpse(sales_clean)## Rows: 113,036
## Columns: 18
## $ Date <date> 2013-11-26, 2015-11-26, 2014-03-23, 2016-03-23, 2014…
## $ Day <int> 26, 26, 23, 23, 15, 15, 22, 22, 22, 22, 30, 30, 15, 1…
## $ Month <chr> "November", "November", "March", "March", "May", "May…
## $ Year <int> 2013, 2015, 2014, 2016, 2014, 2016, 2014, 2016, 2014,…
## $ Customer_Age <int> 19, 19, 49, 49, 47, 47, 47, 47, 35, 35, 32, 32, 34, 3…
## $ Age_Group <fct> Youth (<25), Youth (<25), Adults (35-64), Adults (35-…
## $ Customer_Gender <fct> M, M, M, M, F, F, F, F, M, M, F, F, M, M, M, M, M, M,…
## $ Country <fct> Canada, Canada, Australia, Australia, Australia, Aust…
## $ State <fct> British Columbia, British Columbia, New South Wales, …
## $ Product_Category <fct> Accessories, Accessories, Accessories, Accessories, A…
## $ Sub_Category <fct> Bike Racks, Bike Racks, Bike Racks, Bike Racks, Bike …
## $ Product <fct> "Hitch Rack - 4-Bike", "Hitch Rack - 4-Bike", "Hitch …
## $ Order_Quantity <int> 8, 8, 23, 20, 4, 5, 4, 2, 22, 21, 8, 8, 7, 7, 5, 7, 2…
## $ Unit_Cost <int> 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 4…
## $ Unit_Price <int> 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120…
## $ Profit <int> 590, 590, 1366, 1188, 238, 297, 199, 100, 1096, 1046,…
## $ Cost <int> 360, 360, 1035, 900, 180, 225, 180, 90, 990, 945, 360…
## $ Revenue <int> 950, 950, 2401, 2088, 418, 522, 379, 190, 2086, 1991,…
Selanjutnya kita akan mengecek apakah data kita memiliki missing value atau tidak
sales_clean %>% is.na() %>% colSums()## Date Day Month Year
## 0 0 0 0
## Customer_Age Age_Group Customer_Gender Country
## 0 0 0 0
## State Product_Category Sub_Category Product
## 0 0 0 0
## Order_Quantity Unit_Cost Unit_Price Profit
## 0 0 0 0
## Cost Revenue
## 0 0
Data kita ternyata tidak memiliki missing value Mari kita melanjutkan di tahap exploratory
Kita ingin mengetahui total profit yang dimiliki oleh tiap Negara
sales_clean %>%
group_by(Country) %>%
summarise(Total_Profit = sum(Profit)) %>%
ungroup()Kita ingin mengetahui berapa total Jenis produk yang terjual
sales_count <- sales_clean %>%
group_by(Product_Category) %>%
summarise(Jenis_Produk_Terjual = sum(Order_Quantity)) %>%
ungroup() %>%
arrange(-Jenis_Produk_Terjual)
sales_countKita ingin mengetahui berapa total sub produk yang terjual
sales_count1 <- sales_clean %>%
filter(Product_Category=="Accessories") %>%
group_by(Sub_Category) %>%
summarise(Total_SubProduk_Terjual = sum(Order_Quantity)) %>%
ungroup() %>%
arrange(-Total_SubProduk_Terjual)
sales_count1Kita ingin mengetahui rata-rata profit yang diterima di negara amerika serikat berdasarkan Sub_Category
sales_10 <- sales_clean %>%
filter(Country == "United States") %>%
group_by(Sub_Category) %>%
summarise(mean_profit = mean(Profit)) %>%
ungroup() %>%
arrange(-mean_profit) %>%
head(10)
sales_10Kita ingin mengetahui rata-rata profit berdasarkan setiap tahunnya
sales_profit<- sales_clean %>%
group_by(Year) %>%
summarise(mean_profit = mean(Profit)) %>%
ungroup() %>%
arrange(-mean_profit) %>%
head(10)
sales_profitSelanjutnya mari kita visualisasikan
plot1 <- ggplot(data = sales_count, aes(x = Jenis_Produk_Terjual,
y = reorder(Product_Category, Jenis_Produk_Terjual))) +
geom_col(aes(fill = Jenis_Produk_Terjual)) +
scale_fill_gradient(low="Blue", high="black") +
labs(title = "PRODUCT CATEGORY",
x = "Item Count",
y = NULL) +
theme_minimal() +
theme(legend.position = "none")
plot1sales_profit<- sales_clean %>%
group_by(Year) %>%
summarise(mean_profit = mean(Profit)) %>%
ungroup()
sales_profit <- sales_profit %>%
mutate(
label2 = glue(
"Year: {Year}
Average Profit: {comma(round(mean_profit, 2))}"
))
label_rupiah <- label_dollar(prefix = "$ ", suffix = " M")
plot1 <- ggplot(sales_profit, aes(x=Year, y= mean_profit))+
geom_line(col="blue") +
geom_point(aes(text=label2), col="black") +
labs(
title = "Average Profit",
x = "Year",
y = "Profit"
) +
scale_y_continuous(labels = label_rupiah)+
theme_minimal()## Warning: Ignoring unknown aesthetics: text
ggplotly(plot1, tooltip = "text")sales_10 <- sales_clean %>%
filter(Country == "United States",
Product_Category =='Bikes') %>%
group_by(Sub_Category) %>%
summarise(mean_profit = mean(Profit)) %>%
ungroup() %>%
arrange(-mean_profit)
sales_10 <- sales_10 %>%
mutate(
label = glue(
"Sub Produk: {Sub_Category}
Profit: {round(mean_profit, 2)}"
)
)
plot2 <- ggplot(sales_10, aes(x = reorder(Sub_Category, mean_profit),
y = mean_profit,
text = label)) +
geom_segment(aes(x=reorder(Sub_Category, mean_profit), xend=reorder(Sub_Category, mean_profit), y=0,yend=mean_profit), color="blue") +
geom_point(aes(text = label),color="black") +
coord_flip() +
labs(title = "Items Sold",
x = NULL,
y = "Average Profit") +
theme_minimal()## Warning: Ignoring unknown aesthetics: text
ggplotly(plot2, tooltip = "text")sales_count1 <- sales_clean %>%
filter(Product_Category=="Bikes") %>%
group_by(Sub_Category) %>%
summarise(count=n()) %>%
ungroup() %>%
arrange(-count)
plot3 <- ggplot(data = sales_count1, aes(x = count,
y = reorder(Sub_Category, count))) +
geom_col(aes(fill = count)) +
scale_fill_gradient(low="blue", high="black") +
labs(title = "Total Item Sold",
x = "Item Count",
y = NULL) +
theme_minimal() +
theme(legend.position = "none")
plot3