library(ggplot2)
library(ggridges)
library(tidyverse)
library(dplyr)Tugas Praktikum 3
Data Visualization
1 Comparison - MPG
manu_grup <- mpg |>
group_by(manufacturer) |>
summarise(mean_cty = mean(cty)) |>
arrange(desc(mean_cty)) |>
slice_head(n=10)
manu_grup# A tibble: 10 × 2
manufacturer mean_cty
<chr> <dbl>
1 honda 24.4
2 volkswagen 20.9
3 subaru 19.3
4 hyundai 18.6
5 toyota 18.5
6 nissan 18.1
7 audi 17.6
8 pontiac 17
9 chevrolet 15
10 ford 14
ggplot(
manu_grup,
aes(
x=reorder(manufacturer, mean_cty),
y = mean_cty)) +
geom_col() +
coord_flip() +
labs(
title = "Average Highway Fuel Efficiency by Manufacturer",
x = NULL,
y = "Average highway fuel efficiency (mpg)"
) +
theme_minimal()2 Distribution - Diamonds
ggplot(
diamonds,
aes(
x = price,
y=cut,
fill = cut)) +
geom_density_ridges(
alpha = 0.8,
show.legend= FALSE) +
labs(
title = "Distribution of Diamond Prices by Cut Quality",
x = "Price (USD)",
y = "Density"
) +
theme_minimal() +
theme(legend.position = "bottom")3 Relationship - Diamonds sample
diamonds_sample <- diamonds |>
slice_sample(n = 25000)
ggplot(
diamonds_sample,
aes(
x = carat,
y = price,
color = cut)) +
geom_point(alpha = 0.5) +
labs(
title = "Relationship Between Diamond Weight and Price by Cut Quality",
x = "Diamond weight (carat)",
y = "Price (USD)",
color = "Cut quality"
) +
theme_minimal() 4 Time Series - Economics
ggplot(
economics,
aes(
x = date,
y = psavert)) +
geom_line(linewidth = 0.7) +
scale_y_continuous(
labels = scales::comma) +
labs(
title = "Changes in the Personal Saving Rate",
x = "Time",
y = "Saving Rate"
) 5 Improve Visualization
mpg_class <- mpg |>
group_by(class) |>
summarise(
mean_hwy = mean(hwy),
jumlah = n(),
.groups = "drop"
) |>
arrange(mean_hwy)
mpg_class# A tibble: 7 × 3
class mean_hwy jumlah
<chr> <dbl> <int>
1 pickup 16.9 33
2 suv 18.1 62
3 minivan 22.4 11
4 2seater 24.8 5
5 midsize 27.3 41
6 subcompact 28.1 35
7 compact 28.3 47
plot_before <- ggplot(
mpg_class,
aes(x = class, y = mean_hwy, fill = class)
) +
geom_col() +
coord_cartesian(ylim = c(15, 30)) +
labs(
title = "MPG",
x = "Class",
y = "Value"
) +
theme_minimal() +
theme(legend.position = "none")
plot_before # MASALAH 1:
# Sumbu Y tidak dimulai dari 0 sehingga
# perbedaan antar kategori terlihat terlalu besar.
# MASALAH 2:
# Judul dan label terlalu umum.
# MASALAH 3:
# Banyak warna digunakan hanya sebagai dekorasi.plot_after <- ggplot(
mpg_class,
aes(x = reorder(class, mean_hwy), y = mean_hwy)
) +
geom_col(width = 0.7) +
# Label nilai
geom_text(
aes(label = sprintf("%.1f", mean_hwy)),
hjust = -0.15,
size = 3.8
) +
# Membuat nama kategori lebih mudah dibaca
coord_flip() +
# Sumbu dimulai dari 0
scale_y_continuous(
limits = c(0, 32),
breaks = seq(0, 30, 5)
) +
labs(
title = "Average Highway Fuel Efficiency by Vehicle Class",
subtitle = "Higher values indicate better highway fuel efficiency",
x = "Vehicle class",
y = "Average highway fuel efficiency (mpg)"
) +
theme_minimal(base_size = 12) +
theme(
plot.title = element_text(face = "bold"),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank()
)
plot_after