Dokumen ini mengerjakan seluruh Independent Exercise pada Practicum 3 — Data Visualization. Materi latihan mencakup comparison, distribution, relationship, time series, dan perbaikan visualisasi. Materi juga membahas penggunaan warna, ukuran, transparansi, label, tema, skala, legenda, facet, dan anotasi untuk membuat grafik lebih jelas. fileciteturn0file0L105-L110
library(ggplot2)
library(dplyr)
library(knitr)
set.seed(123)
diamonds_sample <- diamonds %>%
slice_sample(n = 1000)
mpgSoal: Calculate the average cty for
each manufacturer, select the top 10, create a comparison plot, and
summarize the main finding.
ctyavg_cty <- mpg %>%
group_by(manufacturer) %>%
summarise(
avg_cty = mean(cty),
.groups = "drop"
) %>%
arrange(desc(avg_cty))
top10_mpg <- avg_cty %>%
slice_head(n = 10) %>%
arrange(avg_cty)
kable(top10_mpg, digits = 2,
col.names = c("Manufacturer", "Average city MPG"))
| Manufacturer | Average city MPG |
|---|---|
| ford | 14.00 |
| chevrolet | 15.00 |
| pontiac | 17.00 |
| audi | 17.61 |
| nissan | 18.08 |
| toyota | 18.53 |
| hyundai | 18.64 |
| subaru | 19.29 |
| volkswagen | 20.93 |
| honda | 24.44 |
ggplot(top10_mpg,
aes(x = reorder(manufacturer, avg_cty),
y = avg_cty,
fill = avg_cty)) +
geom_col(show.legend = FALSE) +
geom_text(aes(label = round(avg_cty, 1)),
hjust = -0.1, size = 4) +
scale_fill_gradient(low = "#B3D9FF", high = "#1565C0") +
coord_flip() +
labs(title = "Top 10 Manufacturers by Average City MPG",
x = "Manufacturer",
y = "Average city MPG (cty)") +
theme_minimal(base_size = 12) +
theme(plot.title = element_text(face = "bold"),
panel.grid.major.y = element_blank()) +
expand_limits(y = max(top10_mpg$avg_cty) + 2)
Dari 10 manufacturer dengan rata-rata cty tertinggi,
Honda memiliki rata-rata city MPG tertinggi, sekitar
24.44 MPG. Posisi berikutnya adalah Volkswagen (20.93),
Subaru (19.29), Hyundai (18.64), dan Toyota (18.53). Jadi, pada data
mpg, kelompok manufacturer teratas memiliki efisiensi bahan
bakar kota yang lebih tinggi dibandingkan manufacturer lain.
diamondsSoal: Choose one numerical variable, compare its distribution across one categorical variable, improve the plot appearance, and interpret the pattern.
pricecutBoxplot digunakan untuk melihat median, sebaran, dan pencilan harga
pada setiap kategori cut.
diamonds %>%
mutate(cut = factor(cut,
levels = c("Fair", "Good", "Very Good", "Premium", "Ideal"))) %>%
ggplot(aes(x = cut, y = price, fill = cut)) +
geom_boxplot(show.legend = FALSE, outlier.alpha = 0.25) +
scale_y_continuous(labels = scales::dollar_format()) +
labs(title = "Distribution of Diamond Price by Cut",
x = "Cut quality",
y = "Price (USD)") +
theme_minimal(base_size = 12) +
theme(plot.title = element_text(face = "bold"),
panel.grid.major.x = element_blank())
Distribusi harga berbeda antar kategori cut. Median
harga tidak meningkat secara sederhana dari Fair sampai Ideal.
Distribusi juga cukup lebar dan memiliki banyak pencilan. Karena itu,
kategori cut saja tidak cukup untuk menjelaskan harga;
variabel lain seperti carat, color, dan
clarity juga dapat berkaitan dengan price.
diamonds_sampleSoal: Visualize the relationship between
carat and price, add at least one relevant
aesthetic, apply suitable customization, and explain the relationship
shown.
ggplot(diamonds_sample,
aes(x = carat, y = price, color = cut)) +
geom_point(alpha = 0.45, size = 1.7) +
geom_smooth(method = "lm", se = FALSE, color = "grey20") +
scale_y_continuous(labels = scales::dollar_format()) +
labs(title = "Relationship Between Carat and Price",
subtitle = "Sample of 1,000 diamonds",
x = "Carat (weight)",
y = "Price (USD)",
color = "Cut") +
theme_minimal(base_size = 12) +
theme(plot.title = element_text(face = "bold"))
## `geom_smooth()` using formula = 'y ~ x'
Terlihat hubungan positif antara carat
dan price: ketika berat berlian (carat)
meningkat, harga cenderung meningkat. Titik dengan carat kecil umumnya
berada pada harga yang lebih rendah, sedangkan carat yang lebih besar
cenderung memiliki harga lebih tinggi.
Aesthetic warna berdasarkan cut
digunakan untuk membandingkan kategori kualitas potongan. Transparansi
(alpha) membantu mengurangi masalah ketika banyak titik
bertumpuk.
economicsSoal: Visualize psavert over time, use
clear labels and a suitable theme, highlight or annotate a noticeable
change, and provide a short interpretation.
economics %>%
ggplot(aes(x = date, y = psavert)) +
geom_line(color = "#2B6CB0", linewidth = 1) +
geom_point(data = economics %>% filter(date == as.Date("2009-05-01")),
color = "#C53030", size = 3) +
annotate("text",
x = as.Date("2009-05-01"),
y = 8.1,
label = "8.1% in May 2009",
vjust = -0.8,
color = "#C53030") +
labs(title = "Personal Saving Rate Over Time",
subtitle = "U.S. economic time series, 1967–2015",
x = "Date",
y = "Personal saving rate (%)") +
theme_minimal(base_size = 12) +
theme(plot.title = element_text(face = "bold"))
psavert berubah cukup besar dari waktu ke waktu. Salah
satu titik yang disorot adalah 8.1% pada Mei 2009.
Setelah periode tersebut, tingkat tabungan kembali berfluktuasi. Anotasi
digunakan untuk menarik perhatian pada titik tertentu tanpa
menghilangkan pola utama deret waktu.
Soal: 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.
Untuk latihan ini digunakan grafik rata-rata harga berlian
berdasarkan cut.
mean_price_cut <- diamonds %>%
group_by(cut) %>%
summarise(mean_price = mean(price), .groups = "drop")
ggplot(mean_price_cut, aes(x = cut, y = mean_price)) +
geom_col() +
labs(x = "cut", y = "mean_price")
cut
dan mean_price).ggplot(mean_price_cut,
aes(x = cut, y = mean_price, fill = cut)) +
geom_col(show.legend = FALSE) +
geom_text(aes(label = scales::dollar(round(mean_price, 0))),
vjust = -0.3, size = 4) +
scale_y_continuous(
labels = scales::label_dollar(scale = 1 / 1000, suffix = "k"),
expand = expansion(mult = c(0, 0.12))
) +
labs(title = "Average Diamond Price by Cut",
subtitle = "Mean price is sensitive to expensive diamonds",
x = "Cut quality",
y = "Average price (USD)") +
theme_minimal(base_size = 12) +
theme(plot.title = element_text(face = "bold"),
panel.grid.major.x = element_blank())
fill = cut digunakan untuk
membedakan kategori.theme_minimal() membuat
tampilan lebih bersih.Kelima latihan menunjukkan bahwa visualisasi tidak hanya bergantung
pada pemilihan geom. Warna, label, skala, tema,
transparansi, legenda, dan anotasi dapat digunakan untuk
membuat pola data lebih mudah dibaca dan dikomunikasikan.