# Memuat paket yang diperlukan untuk koneksi database dan visualisasi
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
Laporan ini menyajikan banyaknya item untuk tiap product scale dari database “classicmodels”. Proses ini mengintegrasikan SQL untuk pengambilan data dan R untuk visualisasi. # Koneksi Database dan Pengambilan Data Langkah pertama adalah membangun koneksi antara RStudio dan database MySQL menggunakan fungsi dbConnect()
con <- DBI::dbConnect(odbc::odbc(),
Driver = "MySQL ODBC 8.0 ANSI Driver",
Server = "127.0.0.1",
Database = "classicmodels",
UID = "root",
PWD = "1strinyaWOOSEOK", #sesuaikan dg password masing-masing
Port = 3306)
Setelah koneksi terjalin, kita menggunakan blok kode SQL untuk mendapatkan nilai pesanan yang diterima dari pelanggan di negara-negara nordic. Simpan data hasil kueri data dalam suatu data frame R.
# Mendapatkan data yang diperlukan
data1 <- dbGetQuery(con, "select o.orderNumber, country, sum(quantityordered*priceeach) as total_value
from orders o join Orderdetails od on o.OrderNumber=od.OrderNumber
join customers c on c.customerNumber= o.customerNumber
where country in ('Denmark', 'Finland', 'Norway', 'Sweden')
group by o.orderNumber")
data1
## orderNumber country total_value
## 1 10103 Norway 50218.95
## 2 10105 Denmark 53959.21
## 3 10112 Sweden 7674.94
## 4 10141 Finland 29716.86
## 5 10151 Finland 32723.04
## 6 10155 Finland 37602.48
## 7 10158 Norway 1491.38
## 8 10161 Denmark 36164.46
## 9 10167 Sweden 44167.09
## 10 10181 Norway 55069.55
## 11 10188 Norway 29954.91
## 12 10238 Denmark 28211.70
## 13 10239 Finland 16212.59
## 14 10247 Finland 28394.54
## 15 10256 Denmark 4710.73
## 16 10284 Norway 32260.16
## 17 10289 Norway 12538.01
## 18 10291 Sweden 48809.90
## 19 10299 Finland 34341.08
## 20 10301 Norway 36798.88
## 21 10309 Norway 17876.32
## 22 10314 Denmark 53745.34
## 23 10320 Sweden 16799.03
## 24 10325 Norway 34638.14
## 25 10326 Sweden 19206.68
## 26 10327 Denmark 20564.86
## 27 10334 Sweden 23014.17
## 28 10363 Finland 45785.34
## 29 10373 Finland 46770.52
## 30 10377 Finland 23602.90
## 31 10389 Sweden 27966.54
## 32 10406 Denmark 21638.62
Kita menggunakan library ggplot2 untuk membuat barchart. Fungsi geom_his() digunakan untuk melihat banyaknya distribusi dari frekkuansi nilai pesanan tiap product scale.
# Membuat histogram nilai pesanan
ggplot(data1, aes(x = total_value)) +
geom_histogram(aes(fill = "pink")) +
theme_minimal() +
labs(title = "Distribusi Nilai Pesanan di Negara Nordic",
x = "Nilai Pesanan (USD)",
y = "Frekuensi (Banyakya Pesanan")
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.