R Markdown

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

Including Plots

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.

con <- DBI::dbConnect(odbc::odbc(),
                      Driver    = "MySQL ODBC 9.5 ANSI Driver", 
                      Server    = "127.0.0.1",
                      UID       = "root",
                      Database = "classicmodels",
                      PWD       = "@Nasywa18", 
                      Port      = 3306)
library(DBI)
## Warning: package 'DBI' was built under R version 4.4.3
library(odbc)
## Warning: package 'odbc' was built under R version 4.4.3
dt1 <- dbGetQuery (con, "SELECT 
          o.orderNumber,
          c.country,
          SUM(od.quantityOrdered * od.priceEach) AS total_value
        FROM orders o
        JOIN orderdetails od ON o.orderNumber = od.orderNumber
        JOIN customers c ON o.customerNumber = c.customerNumber
        WHERE c.country IN ('Denmark','Finland','Norway','Sweden')
        GROUP BY o.orderNumber")
dt1
##    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
# Membuat histogram dengan format label uang menggunakan labs()
library(ggplot2)
library(scales)

ggplot(dt1, aes(x = total_value)) +
  geom_histogram(aes(fill = after_stat(count)), bins = 25, color = "white") +
  stat_bin(bins = 15, geom = "text", aes(label = after_stat(count)), vjust = -0.5, size = 3) +
  scale_fill_gradient(low = "#132B43", high = "#56B1F7") +
  scale_x_continuous(labels = label_dollar()) +
  scale_y_continuous(expand = expansion(c(0, 0.15))) +
  theme_minimal() +
  labs(title = "Distribusi Nilai Pesanan di Negara Nordic",
    subtitle = "Negara: Denmark, Finland, Norway, Sweden",
    x = "Nilai Pesanan (USD)",
    y = "Frekuensi (Jumlah Order)"
  ) +
  theme(legend.position = "none")