01 / 21

Customizing
ggplot

Learn to transform plain charts into polished visualizations by customizing labels, scales, and themes in R's ggplot2 library.

R Programming

What is ggplot2?

ggplot2 is R's most popular data visualization package. It builds plots in layers: you start with data, map variables to aesthetics, add geometric shapes, and then customize everything from axis labels to color palettes.

Grammar
Based on the "Grammar of Graphics" framework by Leland Wilkinson
Layered
Each element (data, aesthetics, geoms, themes) is a separate layer
Flexible
Every visual detail can be customized with built-in functions

Three Pillars of Customization

When we say "customize ggplot," we mean three specific areas. Mastering these gives you full control over how your charts look and communicate.

1
Labels: titles, subtitles, axis names, and captions that explain your data
2
Scales: control how data values map to visual properties like position, color, and size
3
Themes: adjust non-data elements like fonts, grid lines, backgrounds, and legends

Why Customize Your Plots?

The default ggplot output is functional but not presentation-ready. Customization transforms raw output into visualizations that are clear, professional, and tailored to your audience.

DEFAULT x y → CUSTOMIZED Monthly Revenue Q1 2024 Performance Month $K

Labels with labs()

The labs() function is your one-stop shop for all text labels on a plot. It replaces default variable names with human-readable descriptions.

R
ggplot(data, aes(x = month, y = revenue)) +
  geom_col() +
  labs(
    title    = "Monthly Revenue",
    subtitle = "Q1 2024 Performance",
    x        = "Month",
    y        = "Revenue (USD)",
    caption  = "Source: Sales Database"
  )

labs() vs. Helper Functions

R offers multiple ways to set labels. Here is how they compare:

Function What It Sets Recommended?
labs() All labels at once ✓ Yes
ggtitle() Title + subtitle only Okay, but limited
xlab() / ylab() One axis at a time Verbose for multiple labels

Understanding Scales

Scales control the mapping between your data values and the visual properties you see on the plot. Every aesthetic (x, y, color, size, fill) has a corresponding scale function.

Data Values 0, 50, 100 ... Scale scale_x_continuous() scale_color_manual() Visual pixels, colors

Common Scale Functions

scale_x_continuous()
Set axis limits, breaks, and labels for numeric x-axis
scale_y_log10()
Transform y-axis to logarithmic scale
scale_color_manual()
Assign specific colors to categorical groups
scale_fill_brewer()
Use ColorBrewer palettes for fill aesthetics
R
+ scale_x_continuous(
    limits = c(0, 100),
    breaks = seq(0, 100, 20)
  )

Themes: Controlling Appearance

Themes control non-data ink: backgrounds, grid lines, fonts, legend position, and more. ggplot2 includes several built-in themes, and you can override any element with theme().

1
Built-in themes (e.g., theme_minimal(), theme_classic()) provide a quick starting point
2
theme() lets you fine-tune individual elements like axis text size, legend position, and panel background
3
Element functions like element_text(), element_rect(), and element_line() specify how each component renders

Built-in Themes

theme_gray() Default theme_minimal() Clean, modern theme_classic() No grid lines theme_bw() Black & white More themes: theme_void() No axes at all theme_dark() Dark background theme_light() Light grid lines

How to Customize Step by Step

1
Start with data + geom: build your basic plot with ggplot() and a geom layer
2
Add labels: use labs() to set title, subtitle, axis names, and caption
3
Adjust scales: use scale_*() functions to set limits, breaks, and color mappings
4
Choose a theme: apply a built-in theme (e.g., theme_minimal()) as a baseline
5
Fine-tune with theme(): override specific elements like text size, legend position, and panel background

Example 1: Adding Labels

Let's customize a scatter plot of car data. We start by adding clear, descriptive labels:

R
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "#818cf8", size = 3) +
  labs(
    title    = "Car Weight vs. Fuel Efficiency",
    subtitle = "Heavier cars tend to use more fuel",
    x        = "Weight (1000 lbs)",
    y        = "Miles per Gallon",
    caption  = "Data: mtcars dataset"
  )

Example 2: Customizing Scales

Now let's control the axis range and add custom colors by group:

R
ggplot(mtcars, aes(x = wt, y = mpg,
                    color = factor(cyl))) +
  geom_point(size = 3) +
  scale_x_continuous(
    limits = c(1, 6),
    breaks = seq(1, 6, 1)
  ) +
  scale_color_manual(
    values = c("#818cf8", "#4ade80",
              "#fbbf24"),
    labels = c("4 Cyl", "6 Cyl",
              "8 Cyl")
  )

Example 3: Full Customization

Combining all three pillars into a polished, presentation-ready chart:

R
ggplot(mtcars, aes(x = wt, y = mpg,
                    color = factor(cyl))) +
  geom_point(size = 3) +
  labs(
    title = "Weight vs. Efficiency",
    x = "Weight (1000 lbs)",
    y = "Miles per Gallon",
    color = "Cylinders"
  ) +
  scale_color_brewer(palette = "Set2") +
  theme_minimal() +
  theme(
    plot.title = element_text(
      size = 16, face = "bold"),
    legend.position = "bottom"
  )

Key theme() Arguments

Argument Element Function What It Controls
plot.title element_text() Title font, size, color
axis.text element_text() Tick label appearance
panel.background element_rect() Plot area background
panel.grid.major element_line() Major grid lines
legend.position character string "top", "bottom", "none"

Common Mistakes

✕ theme() Before theme_*()
Placing theme() before a built-in theme like theme_minimal() causes your overrides to be erased
✕ Wrong Scale Type
Using scale_x_discrete() on a continuous variable (or vice versa) produces errors or blank axes
✕ Mismatched Aesthetics
Using scale_color_*() when you mapped fill (not color) in aes(): the scale is silently ignored

Question 1 of 5

Which function is the recommended way to set the plot title, axis labels, and caption all at once?

Question 2 of 5

What is the correct naming pattern for ggplot2 scale functions?

Question 3 of 5

You used aes(fill = category) in your plot but scale_color_manual() to set colors. What will happen?

Question 4 of 5

What is the correct order for applying a built-in theme and custom theme() overrides?

Question 5 of 5

Which element function would you use inside theme() to completely remove the minor grid lines?