PDF

Lesson 2 Exercise Questions: ggplot2 Plot Customization

This document contains practice questions on plot customization using ggplot2.

All questions use datasets available in base R or in ggplot2. Suggested workflow for students:

  1. Attempt each question in your own script or console.
  2. Only then consult the provided solution code.

Note

While one solution is provided per answer, multiple solutions are possible.

Start by loading ggplot2.

if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")

library(ggplot2)

Q1. Using the mtcars dataset, create the following scatter plot:

  • Set the x-axis to hp and the y-axis to mpg.
  • Map cyl to color and am to shape.
  • Increase point size and add a smoothed line (loess) in a different color, without a confidence band.
  • Customize the following:
    • Make axes labels more informative (e.g., “Horsepower (HP)”).
    • Add a plot title (e.g., "Fuel Efficiency vs Horsepower in Motor Cars").
    • Set a minimal theme with a customized base font size = 14.
Q1: Solution
ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point(aes(color = factor(cyl),
                  shape = factor(am)), size = 3) +
  geom_smooth(se = FALSE, method = "loess", color = "black") +
  labs(
    x = "Horsepower (HP)",
    y = "Fuel efficiency (mpg)",
    color = "Cylinders",
    shape = "Transmission (0 = auto, 1 = manual)",
    title = "Fuel Efficiency vs Horsepower in Motor Cars",
  ) +
  theme_minimal(base_size = 14)


Q2: Using diamonds from ggplot2:

  • Create a scatter plot of carat (x-axis) and price (y-axis); set the general transparency of the points to 0.3.
  • Apply a log10 transformation to the y-axis, maintaining the original units on the axis (See ?scale_y_log10()).
  • Limit the x-axis to carats between 0.2 and 2.5.
  • Clean up the labels and add a title.
Q2: Solution
ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point(alpha = 0.3) +
  scale_y_log10() +
  scale_x_continuous(limits = c(0.2, 2.5))+
  labs(
    title = "Diamond price vs carat",
    x = "Carat",
    y = "Price (log10 scale)"
  ) 


Q3: Using ggplot2::mpg, create a scatterplot of displ on the x-axis and hwy on the y-axis.

  • Color points by class.
  • Facet the plot by drv (front, rear, 4-wheel drive).
  • Customize the facet labels using a labeller to replace drv values with more descriptive labels (e.g., "Four-wheel drive", "Front-wheel drive", and "Rear-wheel drive").
  • Arrange the facets in a single row.
  • Rotate the x-axis text by 45 degrees and right-align it.
Q3: Solution
drv_labels <- c(
  "4" = "Four-wheel drive",
  "f" = "Front-wheel drive",
  "r" = "Rear-wheel drive"
)

ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(alpha = 0.7) +
  facet_wrap(~ drv, nrow = 1,
            labeller = labeller(drv = drv_labels)) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1)
  )


Q4. Using ToothGrowth:

  • Convert dose to a factor.
  • Plot len (tooth length) vs dose, with:
    • dose on the x-axis.
    • Points colored by supp and shape also mapped to supp.
  • Customize the plot:
    • Use scale_color_manual() and scale_shape_manual() to assign specific colors and shapes to each supplement type.
    • Combine color and shape into a single legend with a custom title “Supplement type”.
    • Remove the legend background and legend key borders.
Q4: Solution
ToothGrowth$dose <- factor(ToothGrowth$dose)

ggplot(ToothGrowth,
      aes(x = dose, y = len,
          color = supp, shape = supp)) +
  geom_point(size = 3, position = position_jitter(width = 0.05, height = 0)) +
  scale_color_manual(
    name = "Supplement type",
    values = c("OJ" = "#1b9e77", "VC" = "#d95f02")
  ) +
  scale_shape_manual(
    name = "Supplement type",
    values = c("OJ" = 16, "VC" = 17)
  ) +
  labs(
    title = "Tooth length by dose and supplement type",
    x = "Dose (mg)",
    y = "Tooth length"
  ) +
  theme_minimal(base_size = 13) +
  theme(
    legend.background = element_blank(),
    legend.key = element_blank(),
    legend.position = "right"
  )


Q5: Using ggplot2::economics:

  • Create a line plot of date on the x-axis and unemploy (number unemployed) on the y-axis.
  • Add:
    • A vertical line at a chosen date ( lubrdiate::as_date("2000-01-01")) using geom_vline().
    • A text annotation near that line describing the event (See annotate()).
  • Customize the plot:
    • Change the line color and size (within geom_line()).
    • Use a clean theme.
    • Adjust x-axis date breaks and labels (e.g., show ticks every 5 years).
Q5: Solution
library(lubridate)
marker_date <- lubridate::as_date("2000-01-01")

ggplot(economics, aes(x = date, y = unemploy)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_vline(xintercept = marker_date,
            linetype = "dashed",
            color = "red") +
  annotate("text",
          x = marker_date,
          y = max(economics$unemploy) * 0.7,
          label = "Marker date",
          color = "red",
          angle = 90,
          vjust = -0.5) +
  scale_x_date(date_breaks = "5 years", date_labels = "%Y") +
  labs(
    title = "US Unemployment Over Time",
    x = "Year",
    y = "Number unemployed"
  ) +
  theme_minimal(base_size = 13)


Q6. Using any plot you created above (for example, the mtcars plot from Question 1):

  • Save the plot object to a variable, e.g., p.
  • Use ggsave() to:
    • Export the plot as a PNG file.
    • Specify a custom width and height in inches.
    • Set a suitable DPI (e.g., 300).
Q6: Solution
# Create the plot and assign to p
p <- ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point(aes(color = factor(cyl),
                  shape = factor(am)), size = 3) +
  geom_smooth(se = FALSE, method = "loess", color = "black") +
  labs(
    x = "Horsepower (HP)",
    y = "Fuel efficiency (mpg)",
    color = "Cylinders",
    shape = "Transmission (0 = auto, 1 = manual)",
    title = "Fuel Efficiency vs Horsepower in Motor Cars",
  ) +
  theme_minimal(base_size = 14)


# Save as high-resolution PNG
ggsave(
  filename = "mtcars_mpg_hp.png",
  plot = p,
  width = 6,        # inches
  height = 4,       # inches
  dpi = 300
)