1. Comparison - mpg Dataset

Objective: Calculate average cty for each manufacturer, select top 10, create comparison plot.

# Calculate average city mpg by manufacturer
mpg_summary <- mpg %>%
  group_by(manufacturer) %>%
  summarise(avg_cty = mean(cty, na.rm = TRUE)) %>%
  arrange(desc(avg_cty)) %>%
  head(10)

# Create comparison plot
ggplot(mpg_summary, aes(x = reorder(manufacturer, avg_cty), y = avg_cty)) +
  geom_bar(stat = "identity", fill = "gray") +
  geom_text(aes(label = round(avg_cty, 1)), vjust = -0.5, size = 3.5) +
  labs(
    title = "Top 10 Manufacturers by Average City MPG",
    subtitle = "Based on fuel economy data (1999-2008)",
    x = "Manufacturer",
    y = "Average City MPG"
  ) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip()

Summary of main Finding: Honda and Volkswagen stand out as the most fuel-efficient manufacturers, averaging roughly 24–28 city MPG, whereas Chevrolet and Ford trail behind with averages closer to 15–18 MPG. This gap points to a broader pattern: brands that focus on compact, efficiency-oriented vehicles tend to outperform others in city driving conditions.


2. Distribution - diamonds Dataset

Objective: Compare distribution of Caret as my numerical variable and Cut as my categorical variable.

# numerical variable: carat, categorical variable: cut
ggplot(diamonds, aes(x = carat, fill = cut)) +
  geom_histogram(alpha = 0.8, bins = 50, position = "identity") +
  facet_wrap(~cut, ncol = 3) +
  labs(
    title = "Distribution of Diamond Carat by Cut Quality",
    x = "Carat Weight",
    y = "Count",
    fill = "Cut"
  ) +
  theme_minimal() +
  theme(legend.position = "bottom")

Interpretation of patterns: Looking at the histograms, Ideal and Premium diamonds are heavily concentrated below 1 carat, giving their distributions a pronounced right skew, while Fair cut diamonds spread more evenly across carat sizes. Overall, higher-quality cuts cluster around smaller stones, and the median carat weight drops as cut quality improves.


3. Relationship - diamonds_sample

Objective: Visualize relationship between carat and price with customization.

# Create sample if diamonds_sample doesn't exist
set.seed(123)
diamonds_sample <- diamonds %>% sample_n(1000)

# Visualize carat vs price relationship
ggplot(diamonds_sample, aes(x = carat, y = price, color = cut, linewidth = depth)) +
  geom_point(alpha = 0.7) +
  scale_color_brewer(palette = "RdYlGn") +
  scale_size(range = c(1, 4)) +
  labs(
    title = "Diamond Price vs Carat Weight",
    subtitle = "Colored by cut quality, sized by depth percentage",
    x = "Carat Weight",
    y = "Price (USD)",
    color = "Cut Quality",
    size = "Depth %"
  ) +
  theme_bw() +
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    legend.position = "right"
  ) +
  geom_smooth(method = "lm", se = FALSE, color = "black", linetype = "dashed")
## Ignoring unknown labels:
## • size : "Depth %"
## `geom_smooth()` using formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation:
## linewidth.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

Relationship Explanation: Carat weight and price move together strongly and positively, with price climbing exponentially rather than linearly as stones get larger. Bigger diamonds command disproportionately higher prices, and for a given carat weight, better-cut stones (the greener points) tend to sell for more. The dashed trendline reinforces this exponential, rather than linear, growth pattern.


4. Time Series - economics Dataset

Objective: Visualize psavert over time with annotation.

# Identify notable change (2008 financial crisis)
notable_date <- as.Date("2008-10-01")
notable_value <- economics %>%
  filter(date == notable_date) %>%
  pull(psavert)

ggplot(economics, aes(x = date, y = psavert)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_area(alpha = 0.3, fill = "steelblue") +
  geom_vline(xintercept = as.numeric(notable_date), 
             linetype = "dashed", color = "red", linewidth = 1) +
  annotate("rect", 
           xmin = as.Date("2008-01-01"), xmax = as.Date("2009-12-31"),
           ymin = -Inf, ymax = Inf,
           alpha = 0.1, fill = "red") +
  annotate("text", 
           x = as.Date("2008-10-01"), y = 12,
           label = "2008 Financial Crisis\nSavings rate spiked",
           color = "red", fontface = "bold", size = 4) +
  labs(
    title = "US Personal Saving Rate (1967-2015)",
    subtitle = "Percentage of disposable personal income saved",
    x = "Year",
    y = "Personal Saving Rate (%)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 16, face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )
## Warning in scale_x_date(): A <numeric> value was passed to a Date scale.
## ℹ The value was converted to a <Date> object.

Interpretation: During the 2008 financial crisis, the personal saving rate jumped sharply, climbing from about 5% to more than 12%. This surge reflects households pulling back on spending and saving more as economic uncertainty and job insecurity grew. The rate eased gradually after 2012 as confidence returned, though it remained well below the higher baseline levels seen in the 1970s-1980s \((10-15\%)\).


5. Improve a Visualization

Objective: Create a problematic visualization, then improve it.

Original plot with issues to be fixed:

# Visualisation with problems
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point() +
  geom_text(aes(label = model), size = 2) +
  scale_color_manual(values = c("yellow", "lightblue", "pink", "lightgreen", 
                                 "orange", "purple", "brown")) +
  theme_gray()

Problems Identified: Several design choices make this chart hard to read:

  1. The color palette is difficult to distinguish
  2. Text labels overlap heavily
  3. Axis titles and a chart title are missing
  4. The scale doesn’t suit the data
  5. The legend is cluttered
# Improved Version of the Original plot
# only top 15 most fuel-efficient models are labeled to reduce clutter
top15_models <- mpg %>%
  group_by(manufacturer, model, class) %>%
  summarise(avg_hwy = mean(hwy), avg_displ = mean(displ)) %>%
  arrange(desc(avg_hwy)) %>%
  head(15)
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by manufacturer, model, and class.
## ℹ Output is grouped by manufacturer and model.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(manufacturer, model, class))` for per-operation
##   grouping (`?dplyr::dplyr_by`) instead.
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(alpha = 0.6, size = 3) +
  geom_point(data = top15_models, aes(x = avg_displ, y = avg_hwy), 
             size = 5, shape = 21, fill = "white", stroke = 1.5) +
  geom_text_repel(data = top15_models, 
                  aes(x = avg_displ, y = avg_hwy, label = model),
                  size = 3, max.overlaps = 20,
                  box.padding = 0.5, point.padding = 0.5) +
  scale_color_brewer(palette = "Set2", name = "Vehicle Class") +
  labs(
    title = "Engine Size vs Highway Fuel Efficiency",
    subtitle = "Highlighting top 15 most fuel-efficient models (2008 data)",
    caption = "Source: EPA fuel economy data",
    x = "Engine Displacement (Liters)",
    y = "Highway MPG"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(face = "bold", size = 16, hjust = 0.5),
    plot.subtitle = element_text(size = 11, hjust = 0.5, color = "gray40"),
    plot.caption = element_text(size = 9, color = "gray50"),
    panel.grid.minor = element_blank(),
    legend.position = "bottom",
    legend.title = element_text(face = "bold")
  )

Improvements Made: To address these issues, I made the following changes:

  1. Better colors - Switched to the high-contrast ColorBrewer “Set2” palette
  2. Selective labeling - Labeled only the top 15 most fuel-efficient models to cut down on clutter
  3. Clear labels - Included a descriptive title, subtitle, axis labels, and caption
  4. Visual hierarchy - Made top performers stand out with larger, distinct point markers
  5. Better legend - Moved the legend to the bottom and gave it a clear title
  6. Context - Added a caption noting the data source