Name : Naufal Akmal Rizqulloh
NIM : G6401231065
Day, Date (Lecture) : Wednesday, 16 September 2026
(5)
Teacher : Dr. Bagus Sartono, S.Si, M.Si
Grade :
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
data("mpg")
dt1_avg_cty <- mpg %>%
group_by(manufacturer) %>%
summarise(average = mean(cty)) %>%
arrange(desc(average)) %>%
slice_head(n = 10)
dt1_avg_cty
## # A tibble: 10 × 2
## manufacturer average
## <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(data = dt1_avg_cty, aes(x = reorder(manufacturer, average), y = average)) +
geom_col(fill = "steelblue") +
coord_flip() +
labs(
title = "Average of City Mileage (cty) based on Top 10 Manufacturers",
x = "Manufacturer",
y = "Average City Mileage (mpg)"
) +
theme_minimal()
Interpretation: The bar chart shows that Honda has the highest average city mileage among the top 10 manufacturers, followed closely by Volkswagen and Subaru.
library(ggplot2)
data("diamonds")
ggplot(data = diamonds, aes(x = depth, fill = cut, color = cut)) +
geom_density(alpha = 0.3, linewidth = 0.8) +
labs(
title = "Distribution of Depth across Cut Quality",
x = "Depth",
y = "Density",
fill = "Cut Quality",
color = "Cut Quality"
) +
theme_minimal()
Interpretation: The density plot illustrates that diamonds with a “Fair” cut have a wider and more varied distribution of depth, whereas higher quality cuts like “Ideal” are highly concentrated around a specific depth value (around 61-62).
library(ggplot2)
library(dplyr)
data("diamonds")
set.seed(123)
diamonds_sample <- diamonds %>% sample_n(1000)
ggplot(data = diamonds_sample,
aes(x = carat, y = price,
color = cut)) +
geom_point(alpha = 0.7, size = 2) +
scale_color_viridis_d() +
labs(
title = "Relationship between Price and Carat",
subtitle = "Sample of 1000 diamonds",
x = "Carat",
y = "Price",
color = "Cut Quality"
) +
theme_minimal()
Interpretation: There is a strong positive, non-linear relationship between carat and price. As carat increases, the price tends to increase exponentially. Additionally, for a given carat size, diamonds with better cut qualities (e.g., Ideal, Premium) generally command higher prices.
library(ggplot2)
library(dplyr)
data("economics")
ggplot(data = economics,
aes(x = date,
y = psavert)) +
geom_line(color = "darkred", linewidth = 0.7) +
geom_vline(xintercept = as.numeric(as.Date("2008-01-01")), linetype = "dashed", color = "blue", linewidth = 1) +
annotate("text", x = as.Date("2008-01-01"), y = 15, label = "2008 Financial Crisis", hjust = -0.1, color = "blue", fontface = "bold") +
scale_y_continuous(
labels = scales::comma
) +
labs(
title = "Personal Savings Rate (psavert) Over Time",
x = "Year",
y = "Personal Savings Rate (%)"
) +
theme_minimal()
## Warning in scale_x_date(): A <numeric> value was passed to a Date scale.
## ℹ The value was converted to a <Date> object.
Interpretation: The personal savings rate fluctuated significantly over time, showing a general downward trend from the 1970s until the mid-2000s. A noticeable spike occurred around the 2008 financial crisis, which is highlighted with a blue dashed line, likely as people began saving more during economic uncertainty.
# 1. Visualization with 3 presentation problems
ggplot(data = mpg, aes(x = class, y = hwy)) +
geom_point(color = "yellow", size = 1) +
theme_dark() +
labs(title = "hwy vs class", x = "c", y = "h")
# 2. Improved Visualization
ggplot(data = mpg, aes(x = reorder(class, hwy, FUN = median), y = hwy, fill = class)) +
geom_boxplot(alpha = 0.7, show.legend = FALSE) +
scale_fill_viridis_d() +
coord_flip() +
labs(
title = "Highway Mileage Distribution by Vehicle Class",
x = "Vehicle Class",
y = "Highway Mileage (mpg)"
) +
theme_minimal() +
theme(plot.title = element_text(face = "bold"))
Explanation of Changes: 1. Inappropriate
Geometry: The original plot used simple points
(geom_point) for a categorical vs. continuous variable,
causing severe overplotting. It was changed to geom_boxplot
to properly show the distribution and summary statistics. 2.
Poor Aesthetics and Theme: Yellow points on a dark
background (theme_dark()) were hard to read and visually
unappealing. This was replaced with a cleaner
theme_minimal() and a colorblind-friendly palette
(viridis). 3. Unclear Labels and Ordering:
The original labels (“c”, “h”, “hwy vs class”) were uninformative and
the classes were ordered alphabetically. I added clear, descriptive
titles and axis labels, and reordered the boxes by median highway
mileage for better readability.