library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.6.1
library(ggridges)
library(GGally)
library(plotly)
## Warning: package 'plotly' was built under R version 4.6.1
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(maps)
##
## Attaching package: 'maps'
##
## The following object is masked from 'package:purrr':
##
## map
#1. Comparison MPG Calculate the average cty for each manufacturer, select the top 10, create a comparison plot, and summarize the main finding.
data(mpg)
names(mpg)
## [1] "manufacturer" "model" "displ" "year" "cyl"
## [6] "trans" "drv" "cty" "hwy" "fl"
## [11] "class"
head(mpg,10)
## # A tibble: 10 × 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto… f 18 29 p comp…
## 2 audi a4 1.8 1999 4 manu… f 21 29 p comp…
## 3 audi a4 2 2008 4 manu… f 20 31 p comp…
## 4 audi a4 2 2008 4 auto… f 21 30 p comp…
## 5 audi a4 2.8 1999 6 auto… f 16 26 p comp…
## 6 audi a4 2.8 1999 6 manu… f 18 26 p comp…
## 7 audi a4 3.1 2008 6 auto… f 18 27 p comp…
## 8 audi a4 quattro 1.8 1999 4 manu… 4 18 26 p comp…
## 9 audi a4 quattro 1.8 1999 4 auto… 4 16 25 p comp…
## 10 audi a4 quattro 2 2008 4 manu… 4 20 28 p comp…
top10_cty <- mpg %>%
group_by(manufacturer) %>%
summarise(avg_cty= mean(cty))%>%
arrange(desc(avg_cty)) %>%
slice_head(n=10) %>%
mutate(manufacturer = fct_reorder(manufacturer, avg_cty))
ggplot(data = top10_cty, aes(x = manufacturer, y = avg_cty)) +
geom_col(fill = "steelblue") +
coord_flip() +
geom_text(
aes(label = round(avg_cty,1)),
size= 2.5,
hjust = -0.1
)
Kesimpulan: Grafik diatas merupakan 10 manufacturer yang memiliki
rata-rata efisiensi bahan bakar paling bagus yaitu berkisar 24.4 hingga
14 mpg (miles per gallon). Honda merupakan manufacturer paling efisien
dengan 24.4 mpg, disusul Volkswagen dengan 20.9 mpg dan Subaru dengan
19.3 mpg.
#2. Choose one numerical variable, compare its distribution across one categorical variable, improve the plot appearance, and interpret the pattern.
data(diamonds)
names(diamonds)
## [1] "carat" "cut" "color" "clarity" "depth" "table" "price"
## [8] "x" "y" "z"
head(diamonds,10)
## # A tibble: 10 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
diamonds$clarity <- factor(
diamonds$clarity,
levels = c("IF", "VVS1", "VVS2", "VS1",
"VS2", "SI1", "SI2", "I1")
)
ggplot(data = diamonds, aes(x = clarity, y = price, fill = clarity)) +
geom_boxplot(
alpha = 0.7,
outlier.size = 0.5,
outlier.alpha = 0.3,
show.legend = FALSE
) +
labs(
title = "Distribusi Harga Berlian Berdasarkan Tingkat Kejernihan (Clarity)",
x = "Tingkat Kejernihan (Clarity)",
y = "Harga Berlian (USD)"
) +
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 12),
axis.title = element_text(face = "bold")
)
Kesimpulan:
- Bentuk Distribusi harga berlian pada seluruh
tingkat kejernihan (clarity) memiliki pencilan (outliers) yang memanjang
ke arah harga tinggi di atas $10.000.
-Kategori kejernihan tingkat
menengah seperti VS1, VS2, dan SI1 memiliki rentang antarkuartil yang
relatif lebih lebar dibandingkan kategori terendah (I1) atau tertinggi
(IF), hal menunjukkan variasi harga yang lebih tinggi pada kelompok
tersebut sejalan dengan tingginya kotak tersebut.
- Sedangkan
Berlian dengan tingkat kejernihan tertinggi (IF) memiliki nilai median
harga yang cenderung lebih rendah dibandingkan kejernihan tingkat
menengah / rendah. Hal ini menunjukkan bahwa harga berlian tidak
ditentukan oleh kejernihan saja namun juga dipengaruhi oleh variabel
lain misalnya hasil potongan (cut), carat, dan warna serta lainnya.
#3. Visualize the relationship between carat and price, add at least one relevant aesthetic, apply suitable customization, and explain the relationship show
set.seed(123)
diamonds_sample <- diamonds %>%
slice_sample(n = 3000)
ggplot(data = diamonds_sample, aes(x = carat, y = price, color = cut)) +
geom_point(alpha = 0.7, size = 1.2) +
labs(
title = "Hubungan Antara Berat (Carat) dan Harga Berlian",
x = "Berat Berlian (Carat)",
y = "Harga Berlian (USD)",
color = "Kualitas Potongan"
) +
geom_smooth(
aes(group = 1),
color = "black",
linetype = "dashed",
linewidth = 0.3
)+
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 12),
legend.position = "bottom"
)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Kesimpulan:
- Terdapat hubungan positif yang kuat antara berat
berlian (carat) dan harga (price). Namun, hubungannya tidak bersifat
linier lurus, melainkan seperti kurva nonlinear.
-Terdapat variasi
harga diamond dengan berat carat yang sama. Hal ini juga dijelaskan
dengan adanya variasi/perbedaan warna berdasarkan kategori hasil
pemotongan/cut juga menunjukkan adanya variasi harga berlian.
-Data
berat berlian/ carat lebih banyak pada ukuran bulat atau pecahan pas
(seperti 0.5, 1.5, dan 2.0 karat)
#4. Visualize psavert over time, use clear labels and a suitable theme, highlight or annotate a noticeable change, and provide a short interpretation.
min_psavert <- economics %>%
filter(psavert == min(psavert))
min_psavert
## # A tibble: 1 × 6
## date pce pop psavert uempmed unemploy
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2005-07-01 8830. 296186 2.2 8.8 7406
max_psavert <- economics %>%
filter(psavert == max(psavert))
max_psavert
## # A tibble: 1 × 6
## date pce pop psavert uempmed unemploy
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1975-05-01 1019. 215523 17.3 9.4 8433
economics_2008 <- economics %>%
filter(year(date) == 2008)
economics_2008
## # A tibble: 12 × 6
## date pce pop psavert uempmed unemploy
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2008-01-01 9930 303506 3.7 9 7685
## 2 2008-02-01 9913. 303711 4.1 8.7 7497
## 3 2008-03-01 9959. 303907 4 8.7 7822
## 4 2008-04-01 9997. 304117 3.4 9.4 7637
## 5 2008-05-01 10054. 304323 7.8 7.9 8395
## 6 2008-06-01 10108. 304556 5.5 9 8575
## 7 2008-07-01 10105. 304798 4.4 9.7 8937
## 8 2008-08-01 10095. 305045 3.8 9.7 9438
## 9 2008-09-01 10044. 305309 4.7 10.2 9494
## 10 2008-10-01 9960. 305554 5.5 10.4 10074
## 11 2008-11-01 9821. 305786 6.4 9.8 10538
## 12 2008-12-01 9731. 306004 6.4 10.5 11286
ggplot(data = economics, aes(x = date, y = psavert)) +
geom_line(color = "steelblue", linewidth = 0.9) +
geom_point(
data = min_psavert,
aes(x = date, y = psavert),
color = "indianred2",
size = 2
) +
annotate(
"text",
x = min_psavert$date,
y = min_psavert$psavert - 1.2,
label = paste0("Titik Terendah: ", min_psavert$psavert, "% (2005)"),
color = "indianred2",
fontface = "bold",
size = 3
) +
geom_vline(xintercept = as.Date("2008-04-01"), linetype = "dashed", color = "darkorange2", linewidth = 0.3) +
annotate(
"text",
x = as.Date("2008-04-01"),
y= 14,
label= "Krisis Keuangan 2008 (Peningkatan Psavert)",
color= "darkorange2",
fontface = "bold",
size = 2.5
) +
labs(
title = "Tren Tingkat Tabungan Pribadi AS (1967 - 2015)",
x = "Tahun",
y = "Tingkat Tabungan Pribadi (%)"
) +
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 12),
axis.title = element_text(face = "bold")
)
Kesimpulan:
- Pada awal tahun 1970-an hingga pertengahan
2000-an, tingkat tabungan masyarakat AS menunjukkan tren penurunan. Dari
puncaknya di kisaran 17% tahun 1975 hingga mencapai titik terendah
sebesar 2,2% pada tahun 2005.
- Setelah mencapai titik terendah
pada tahun 2005 kemudian terjadi Krisis Ekonomi Global 2008, sehingga
terjadi pembalikan arah pola (rebound). Tingkat tabungan pribadi kembali
meningkat ke kisaran 6%–8%.Fenomena ini terjadi karena krisis keuangan
global pada tahun 2008 memicu pergeseran perilaku masyarakat untuk
cenderung memperbanyak tabungan/ cadangan keuangan daripada mengeluarkan
uang.
#5. Create one visualization with at least three presentation problems, then redesign it using improvements such as color, theme, scale, labels, legend, or annotation, and briefly explain the changes.
#Grafik 1 yang akan diperbaiki
ggplot(data = top10_cty,
aes(x = manufacturer, y = avg_cty)) +
geom_col(fill = "steelblue") +
coord_flip() +
geom_text(
aes(label = round(avg_cty,1)),
size= 2.5,
hjust = -0.1
)
#Grafik 1 yang telah diperbaiki
ggplot(data = top10_cty,
aes(x = manufacturer, y = avg_cty, fill = avg_cty)) +
geom_col(show.legend = FALSE,
width = 0.7
) +
coord_flip() +
geom_text(
aes(label = round(avg_cty,1)),
size= 3.5,
hjust = -0.25,
fontface= "bold",
color = "grey20"
)+
scale_fill_gradient(
low = "#BFDDF2",
high = "#123B5D"
) +
scale_y_continuous(limits = c(0, 27),
breaks = seq(0, 25, 5),
expand = c(0, 0)
) +
labs(
title = "Top 10 Produsen Mobil dengan Efisiensi BBM Kota Tertinggi",
subtitle = "Rata-rata efisiensi bahan bakar dalam kondisi berkendara di kota",
x = "Manufacturer",
y = "Rata-rata Efisiensi MPG"
) +
theme_minimal()+
theme(
plot.title = element_text(
face = "bold",
size = 16,
hjust = 0.9,
color = "#123B5D"
),
plot.subtitle = element_text(
size = 12,
color = "grey40",
hjust = 0.5
),
axis.text.y = element_text(
size = 11,
face = "bold",
color = "grey30"
),
axis.text.x = element_text(
size = 11,
color = "grey30"
),
axis.title.x = element_text(
size = 11,
color = "grey30",
),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
)
Perubahan yang dilakukan:
- Menambahkan gradasi warna
berdasarkan nilai avg_cty yang lebih tinggi ditampilkan dengan warna
yang lebih gelap dan nilai yang lebih rendah dengan warna yang lebih
muda.
- Memperjelas informasi nilai pada setiap batang dengan
memperbesar ukuran menjadi 3.5 dan serta format bold.
- Memperjelas
skala sumbu dengan scale_y_continuous() break per 5 unit sehingga
memudahkan membaca nilai.
- Menambahkan judul dan subtitle yang
informatif dimana sebelumnya tidak ada. Judul dibuat lebih besar, bold,
dan menggunakan warna yang konsisten dengan grafik. Sedangkan subtitle
warna lebih pudar dan ukuran lebih kecil.
- Memperbaiki label
manufacturer dibuat lebih besar dan bold sehingga lebih mudah dibaca.
Perbaikan label sumbu x yang sebelumnya hanya nama var (avg_cty) menjadi
“Rata-rata Efisiensi MPG”.
- Mengurangi elemen visual yang tidak
diperlukan seperti Gridline horizontal dan minor dihilangkan agar grafik
terlihat lebih bersih dan fokus pada perbandingan batang.