"NAMA : Jane Ivana Sinaga"
## [1] "NAMA : Jane Ivana Sinaga"
"NIM : M0725067"
## [1] "NIM : M0725067"
"Kelas : E"
## [1] "Kelas : E"
- Menghubungkan koneksi R dengan SQL
con <- DBI::dbConnect(odbc::odbc(),
Driver = "MySQL ODBC 8.0 ANSI Driver",
Server = "127.0.0.1",
Database = "coffee_chain",
UID = "root",
PWD = "admin123",
Port = 3306)
# R dihubungkan dengan database MySQL coffee_chain menggunakan fungsi dbConnect(). Proses ini penting karena menjadi langkah awal agar data yang tersimpan dalam database dapat diakses langsung dari R untuk dapat menganalisis dan membuat visualisasi #
- Memuat Packages yang dibutuhkan dalam memvisualisasi dan
menganalisis data
library(DBI)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
library(scales)
## Warning: package 'scales' was built under R version 4.5.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.2
##
## 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
# Packages yang digunakan mendukung proses analisis data secara menyeluruh. DBI dipakai untuk koneksi database, ggplot2 untuk membuat grafik, scales untuk memformat angka agar lebih mudah dibaca, dan dplyr untuk membantu pengolahan data. Dengan memuat package ini, proses analisis menjadi lebih sistematis dan efisien #
- Menambahkan Query data yang telah dianalisis dalam SQL ke dalam
R
q1 <- "
SELECT
DATE_FORMAT(STR_TO_DATE(`Date`, '%Y-%m-%d %H:%i:%s'), '%Y-%m') AS month,
SUM(Sales) AS total_sales,
SUM(Profit) AS total_profit,
SUM(Margin) AS total_margin
FROM factTable
GROUP BY month
ORDER BY month;
"
trend_df <- dbGetQuery(con, q1)
trend_df$month <- as.Date(paste0(trend_df$month, '-01'))
trend_df
## month total_sales total_profit total_margin
## 1 2012-01-01 31555 8041 17378
## 2 2012-02-01 32092 8369 17762
## 3 2012-03-01 32245 8365 17803
## 4 2012-04-01 32943 8670 18242
## 5 2012-05-01 33692 8947 18618
## 6 2012-06-01 35125 9571 19457
## 7 2012-07-01 36161 9905 20012
## 8 2012-08-01 36029 9566 19736
## 9 2012-09-01 33092 8508 18124
## 10 2012-10-01 32849 8674 18186
## 11 2012-11-01 32003 8399 17766
## 12 2012-12-01 33373 8811 18435
## 13 2013-01-01 35316 12524 17378
## 14 2013-02-01 34192 12419 17762
## 15 2013-03-01 34355 12415 17803
## 16 2013-04-01 35112 12863 18242
## 17 2013-05-01 33394 12348 18618
## 18 2013-06-01 34807 13218 19457
## 19 2013-07-01 35830 13671 20012
## 20 2013-08-01 35707 13205 19736
## 21 2013-09-01 35269 12627 18124
## 22 2013-10-01 34987 12878 18186
## 23 2013-11-01 34103 12460 17766
## 24 2013-12-01 35580 13089 18435
- Membuat visualisasi tren total profit per bulan
ggplot(trend_df, aes(x = month, y = total_profit)) +
geom_line(linewidth = 1) +
geom_point(size = 2) +
scale_y_continuous(labels = comma) +
labs(
title = "Tren Total Profit Bulanan",
x = "Bulan",
y = "Total Profit"
) +
theme_minimal()

# Grafik garis total profit bulanan menunjukkan perkembangan keuntungan perusahaan pada setiap bulan. Visualisasi ini membantu melihat apakah profit bergerak sejalan dengan penjualan atau justru mengalami pola yang berbeda #
- Mengambil data total sales per produk
q2 <- "SELECT p.Product,p.`Product Line`,p.`Product Type`,p.Type,
SUM(f.Sales) AS total_sales,
SUM(f.Profit) AS total_profit,
SUM(f.Margin) AS total_margin
FROM factTable f
JOIN Product p
ON f.ProductId = p.ProductId
GROUP BY
p.Product,
p.`Product Line`,
p.`Product Type`,
p.Type
ORDER BY total_sales DESC;
"
product_df <- dbGetQuery(con, q2)
product_df
## Product Product Line Product Type Type total_sales total_profit
## 1 Colombian Beans Coffee Regular 128311 55804
## 2 Lemon Leaves Herbal Tea Decaf 95926 29869
## 3 Caffe Mocha Beans Espresso Regular 84904 17678
## 4 Decaf Espresso Beans Espresso Decaf 78162 29502
## 5 Chamomile Leaves Herbal Tea Decaf 75578 27231
## 6 Darjeeling Leaves Tea Regular 73151 29053
## 7 Earl Grey Leaves Tea Regular 66772 24164
## 8 Decaf Irish Cream Beans Coffee Decaf 62248 13989
## 9 Caffe Latte Beans Espresso Regular 35899 11375
## 10 Mint Leaves Herbal Tea Decaf 35710 6154
## 11 Green Tea Leaves Tea Regular 32850 -231
## 12 Amaretto Beans Coffee Regular 26269 4890
## 13 Regular Espresso Beans Espresso Regular 24031 10065
## total_margin
## 1 77252
## 2 52418
## 3 45084
## 4 43162
## 5 42552
## 6 41064
## 7 36786
## 8 31370
## 9 19824
## 10 15030
## 11 12444
## 12 12950
## 13 13102
# Query kedua digunakan untuk menggabungkan tabel factTable dan Product, lalu menghitung total Sales, Profit, dan Margin untuk masing-masing produk. Hasilnya menunjukkan kontribusi tiap produk terhadap performa bisnis secara keseluruhan #
- Membuat visualisasi top 10 produk berdasarkan sales
q3 <- product_df %>%
arrange(desc(total_sales)) %>%
slice_head(n = 10)
ggplot(q3, aes(x = reorder(Product, total_sales), y = total_sales)) +
geom_col() +
coord_flip() +
scale_y_continuous(labels = comma) +
labs(
title = "Top 10 Product by Sales",
x = "Product",
y = "Total Sales"
) +
theme_minimal()

# menampilkan 10 produk dengan total penjualan tertinggi dalam bentuk diagram batang. Data terlebih dahulu diurutkan berdasarkan total_sales dari yang terbesar, kemudian diambil 10 produk teratas. Grafik ini membantu menunjukkan produk mana yang memberikan kontribusi penjualan paling besar #
- Mengambil data sales variance per produk
q4 <- "
SELECT p.Product, SUM(f.Sales)
AS actual_sales,
SUM(f.`Budget Sales`) AS budget_sales,
SUM(f.`Sales`) - SUM(f.`Budget Sales`) AS sales_variance
FROM factTable f
JOIN Product p
ON f.ProductId = p.ProductId
GROUP BY p.Product
ORDER BY sales_variance DESC;
"
variance_df <- dbGetQuery(con, q4)
variance_df
## Product actual_sales budget_sales sales_variance
## 1 Lemon 95926 78300 17626
## 2 Earl Grey 66772 50900 15872
## 3 Darjeeling 73151 57360 15791
## 4 Chamomile 75578 63840 11738
## 5 Green Tea 32850 25340 7510
## 6 Mint 35710 28320 7390
## 7 Caffe Latte 35899 30540 5359
## 8 Decaf Espresso 78162 75720 2442
## 9 Regular Espresso 24031 22620 1411
## 10 Caffe Mocha 84904 84600 304
## 11 Amaretto 26269 27200 -931
## 12 Decaf Irish Cream 62248 67040 -4792
## 13 Colombian 128311 134380 -6069
# mengambil data penjualan aktual (actual_sales), target penjualan (budget_sales), dan selisih keduanya (sales_variance) pada setiap produk. Hasil query kemudian disimpan ke dalam data frame variance_df agar dapat dianalisis dan divisualisasikan di R #
q5 <- variance_df %>%
arrange(desc(sales_variance)) %>%
slice_head(n = 10)
ggplot(q5, aes(x = reorder(Product, -sales_variance), y = sales_variance)) +
geom_col() +
scale_y_continuous(labels = comma) +
labs(
title = "Top 10 Produk berdasarkan Sales Variance",
x = "Produk",
y = "Sales Variance"
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

# memilih 10 produk dengan sales variance tertinggi, lalu menampilkannya dalam bentuk diagram batang, ini membantu melihat produk mana yang paling jauh melampaui target penjualan #