R Markdown

library(readxl)
unzip("online+retail.zip", list = TRUE)
##                 Name   Length                Date
## 1 Online Retail.xlsx 23715344 2023-05-22 15:20:00
unzip("online+retail.zip", exdir = "data")
library(readxl)
Online_Retail.xlsx <- readxl::read_excel("Online_Retail.xlsx")
install.packages("ggplot2", repos = "https://cloud.r-project.org/")
## Installing package into 'C:/Users/Muhammad Hafizh Ilmi/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Muhammad Hafizh Ilmi\AppData\Local\Temp\RtmpUvjxwE\downloaded_packages
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
# Menghitung jumlah transaksi per negara
country_counts <- as.data.frame(table(Online_Retail.xlsx$Country))
colnames(country_counts) <- c("Country", "Count")
country_counts <- country_counts[order(-country_counts$Count), ]

# Membuat diagram batang
ggplot(country_counts, aes(x = reorder(Country, -Count), y = Count)) +
  geom_bar(stat = "identity", fill = "darkgoldenrod") +
  scale_y_continuous(labels = scales::comma) +
  labs(title = "Jumlah Transaksi per Negara", x = "Negara", y = "Jumlah Transaksi") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

library(dplyr)
## 
## 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(readxl)
data_retail <- read_excel("online_retail.xlsx")
getwd()
## [1] "C:/Folderan PSD"
retail_data <- read_excel("online_retail.xlsx")
# Pembersihan Data Awal
# Menghapus baris di mana Quantity bernilai negatif (biasanya pengembalian/retur)
data_bersih <- data_retail %>%
  filter(Quantity > 0)
# Menghitung Total Kuantitas Terjual untuk Setiap Produk
produk_terlaris <- data_bersih %>%
  group_by(Description) %>%
  summarise(Total_Quantity_Sold = sum(Quantity, na.rm = TRUE)) %>%
  # Mengurutkan dari yang paling laris
  arrange(desc(Total_Quantity_Sold)) %>%
  # Mengambil 10 produk teratas
  head(10)
# Menampilkan Hasil (Top 10 Produk Terlaris)
print("--- 10 Produk Paling Laris Berdasarkan Total Kuantitas Terjual ---")
## [1] "--- 10 Produk Paling Laris Berdasarkan Total Kuantitas Terjual ---"
print(produk_terlaris)
## # A tibble: 10 × 2
##    Description                        Total_Quantity_Sold
##    <chr>                                            <dbl>
##  1 PAPER CRAFT , LITTLE BIRDIE                      80995
##  2 MEDIUM CERAMIC TOP STORAGE JAR                   78033
##  3 WORLD WAR 2 GLIDERS ASSTD DESIGNS                55047
##  4 JUMBO BAG RED RETROSPOT                          48478
##  5 WHITE HANGING HEART T-LIGHT HOLDER               37895
##  6 POPCORN HOLDER                                   36761
##  7 ASSORTED COLOUR BIRD ORNAMENT                    36461
##  8 PACK OF 72 RETROSPOT CAKE CASES                  36419
##  9 <NA>                                             32547
## 10 RABBIT NIGHT LIGHT                               30788
# Visualisasi (Opsional: Bar Plot)
# Mengubah kolom Description menjadi faktor dan menyusun ulang berdasarkan Total_Quantity_Sold
produk_terlaris$Description <- factor(produk_terlaris$Description, levels = produk_terlaris$Description)

plot_terlaris <- ggplot(produk_terlaris, aes(x = Description, y = Total_Quantity_Sold)) +
  geom_bar(stat = "identity", fill = "chocolate") +
  theme_minimal() +
  # Memutar teks di sumbu x agar mudah dibaca
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(
    title = "Top 10 Produk Paling Laris",
    x = "Deskripsi Produk",
    y = "Total Kuantitas Terjual"
  )

# Menampilkan plot
print(plot_terlaris)

# Menghitung jumlah CustomerID unik
jumlah_customer <- data_bersih %>%
  summarise(Jumlah_Customer_Unik = n_distinct(CustomerID))
# Pembersihan Data dan Penghitungan CustomerID Unik
# Menghapus baris dengan CustomerID NA/kosong
data_bersih <- data_retail %>%
  filter(!is.na(CustomerID))
# Menghitung jumlah CustomerID yang unik
jumlah_customer_unik <- data_bersih %>%
  summarise(Jumlah_Pelanggan = n_distinct(CustomerID))
# Mengambil nilai tunggalnya
jumlah_total <- jumlah_customer_unik$Jumlah_Pelanggan
# Menampilkan Hasil
print(paste("Jumlah CustomerID unik pada data tersebut adalah:", jumlah_total))
## [1] "Jumlah CustomerID unik pada data tersebut adalah: 4372"
# Membuat dataframe untuk visualisasi
data_plot <- data.frame(
  Kategori = "Total Customer Unik",
  Jumlah = jumlah_total
)

plot_customer <- ggplot(data_plot, aes(x = Kategori, y = Jumlah)) +
  # Membuat diagram batang
  geom_bar(stat = "identity", fill = "sienna") +
  # Menambahkan nilai di atas batang
  geom_text(aes(label = Jumlah), vjust = -0.5, size = 6, fontface = "bold") +
  theme_minimal() +
  labs(
    title = "Total CustomerID Unik",
    x = "", # Sumbu X dikosongkan karena hanya ada satu kategori
    y = "Jumlah Pelanggan"
  ) +
  # Mengatur batas sumbu Y agar nilai terlihat jelas
  ylim(0, jumlah_total * 1.1)

# Menampilkan plot
print(plot_customer)

# Pembersihan Data dan Penghitungan Total Kuantitas
# Filter data: hanya menyertakan transaksi penjualan (Quantity > 0)
data_penjualan <- data_retail %>%
  filter(Quantity > 0)
# Menghitung total kuantitas terjual
total_quantity_sold <- data_penjualan %>%
  summarise(Total_Quantity = sum(Quantity, na.rm = TRUE))
# Mengambil nilai tunggalnya
jumlah_total_quantity <- total_quantity_sold$Total_Quantity
# Menampilkan Hasil
# Format angka dengan pemisah ribuan agar mudah dibaca
formatted_quantity <- format(jumlah_total_quantity, big.mark = ",", scientific = FALSE)
print(paste("Total keseluruhan kuantitas produk yang terjual (tanpa retur) adalah:", formatted_quantity))
## [1] "Total keseluruhan kuantitas produk yang terjual (tanpa retur) adalah: 5,660,981"
library(ggplot2)
library(scales) # Pastikan Anda memuat library scales

# Data contoh untuk plot
data_plot <- data.frame(
  Kategori = "Total Kuantitas Terjual",
  Jumlah = 6000000
)

# Visualisasi
plot_quantity <- ggplot(data_plot, aes(x = Kategori, y = Jumlah)) +
  geom_bar(stat = "identity", fill = "saddlebrown") +
  # Ini adalah kodenya:
  scale_y_continuous(labels = scales::comma) + 
  # ... kode plot lainnya
  labs(
    title = "Total Kuantitas Produk",
    y = "Jumlah Kuantitas"
  )

print(plot_quantity)