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(odbc)
## Warning: package 'odbc' was built under R version 4.5.2
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.
con <- DBI::dbConnect(odbc::odbc(),
Driver = "MySQL ODBC 8.0 ANSI Driver",
Server = "127.0.0.1",
Database = "classicmodels",
UID = "root",
PWD = "27112006",
Port = 3306)
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 o.customernumber = c.customernumber
WHERE c.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 histogram. Fungsi geom_histogram() digunakan untuk melihat banyaknya item tiap product scale.
ggplot(data1, aes(x = TOTAL_VALUE)) +
geom_histogram(aes(fill=after_stat(count)), bin = 15, color = "white") +
stat_bin(bins=15, geom = "text", aes(label = after_stat(count), vjust = -0.5, size = 3)) +
theme_minimal() +
labs(title = "DISTRIBUSI NILAI PESANAN DI NEGARA NORDIK",
subtitle = "Negara ; Norwegia, Denmark, Finland, Swedia",
x = "Nilai Pesanan (USD)",
y = "Frekuensi (Jumlah Pesanan)") +
scale_fill_gradient(low = "lightgreen", high = "darkgreen") +
scale_x_continuous(labels=label_dollar()) +
scale_y_continuous(expand=expansion(mult = c(0, 0.15))) +
theme(legend.position = "none")
## Warning in geom_histogram(aes(fill = after_stat(count)), bin = 15, color =
## "white"): Ignoring unknown parameters: `bin`
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.