This document presents the solutions for Practicum 3 - Data Visualization.
The visualizations were created using R and the ggplot2
package.
#Exercise 1: Comparison ##Objective
Calculate the average city mileage (cty) for each
manufacturer, select the top 10 manufacturers, and visualize the
comparison.
data(mpg)
mpg_avg <- mpg %>%
group_by(manufacturer) %>%
summarise(mean_cty = mean(cty)) %>%
arrange(desc(mean_cty))
top10_cty <- mpg_avg %>%
slice_head(n = 10)
ggplot(top10_cty,
aes(x = reorder(manufacturer, mean_cty),
y = mean_cty)) +
geom_col(fill = "#4DBBD5") +
coord_flip() +
labs(
title = "Top 10 Manufacturers by Average City MPG",
x = "Manufacturer",
y = "Average City MPG"
) +
theme_minimal()
###Interpretation
The chart compares the average city mileage among the top 10
manufacturers. Manufacturers with higher average cty values
indicate better city fuel efficiency within this group.
#Exercise 2: Distribution
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) +
geom_boxplot(alpha = 0.7) +
labs(
title = "Distribution of Diamond Prices by Cut",
x = "Cut",
y = "Price"
) +
theme_minimal() +
theme(legend.position = "none")
###Interpretation
The boxplot shows how diamond prices are distributed across different cut categories. The distributions show differences in their median prices and variability, with several extreme values appearing in the higher-price range.
#Exercise 3: Relationship
set.seed(123)
diamonds_sample <- diamonds %>%
slice_sample(n = 1000)
ggplot(diamonds_sample,
aes(x = carat, y = price, color = cut)) +
geom_point(alpha = 0.6, size = 2) +
labs(
title = "Relationship Between Carat and Price",
x = "Carat",
y = "Price",
color = "Cut"
) +
theme_minimal()
###Interpretation
The scatter plot shows a positive relationship between carat and price. In general, diamonds with larger carat values tend to have higher prices, although there is considerable variation among observations.
#Exercise 4: Time series
economics_change <- economics %>%
mutate(change = psavert - lag(psavert))
largest_change <- economics_change %>%
filter(!is.na(change)) %>%
slice_max(order_by = abs(change), n = 1)
ggplot(economics_change, aes(x = date, y = psavert)) +
geom_line(
color = "#3B82F6",
linewidth = 1
) +
geom_point(
data = largest_change,
size = 4,
color = "#EF476F"
) +
geom_label(
data = largest_change,
aes(
label = paste0(
"Largest month-to-month change\n",
format(date, "%b %Y"),
"\nSaving rate: ", round(psavert, 1), "%"
)
),
color = "#C2185B",
fill = "#FFE4EC",
fontface = "bold",
size = 3.5,
hjust = 1,
vjust = 2
) +
labs(
title = "Personal Saving Rate Over Time",
x = "Year",
y = "Personal Saving Rate (%)"
) +
theme_classic() +
theme(
plot.title = element_text(
face = "bold",
size = 16
),
axis.title = element_text(
face = "bold"
)
)
###Interpretation
The personal saving rate generally shows a declining pattern over the observed period, although several fluctuations occur. The highlighted point indicates the largest month-to-month change identified in the dataset.
#Exercise 5: Improving a visualization ##Before Improvement
ggplot(mpg,
aes(x = displ,
y = hwy)) +
geom_point()
##After improvement
ggplot(mpg,
aes(x = displ,
y = hwy,
color = class)) +
geom_point(
size = 2.5,
alpha = 0.8
) +
labs(
title = "Engine Size and Highway Fuel Efficiency",
subtitle = "Fuel efficiency varies across car classes",
x = "Engine Displacement (liters)",
y = "Highway Fuel Efficiency (mpg)",
color = "Car Class"
) +
theme_minimal()
###Improvements
The original visualization had several presentation problems, including limited color differentiation, unclear labels, and a basic default appearance.
The redesigned visualization improves the presentation by using color, transparency, clearer labels, and a cleaner theme. These changes make the visualization easier to read and interpret.