Scales: Defining How Data is Mapped to Aesthetics
Scales determine how raw data gets converted into colors, sizes, axes, and more. They’re the link between data values and their visual representation.
From color palettes to log transforms, you’ll learn how to take full control over the look and feel of your plots — without changing the data itself.
🧠 Map ’n’ Shape
When you specify an aesthetic like x = wday
or color = cyl
, you’re telling ggplot2 what variable to map to the respective aesthetic — andggplot2
encodes them accordingly.
But what defines how the values of that column are translated into visual properties? That’s where scales come in.
Each aesthetic (like color
, size
, or x
) has a corresponding scale that controls how the data is visually represented.
Together, aesthetics and scales form a two-part system:
aes()
maps your data columns to visual properties.scale_*()
controls how those properties appear in the final plot.
The names of scale functions (and there are a ton!) always reference the respective aesthetic and often a specific type of data or behavior:
scale_[aesthetic]_[type]()
Feels abstract? Let’s look at a few examples:
scale_x_date()
Controls how date variables are represented along the x-axis.scale_y_continuous()
Controls how continuous (numeric) variables are displayed on the y-axis.scale_color_discrete()
Controls the encoding of discrete (categorical) variables mapped to the color aesthetic.
Based on the mapping you’ve specified, ggplot2
applies appropriate default scales (if you don't specify them yourself) — behind the scenes, to make your plot work:
🎚️ Controlling Scales
If you want to tweak how a scale behaves, you can simply modify its settings — or replace it with the scale_*()
function that fits your needs.
Some common reasons to modify or replace a scale:
- Changing axis limits — like filtering the data range or capping extreme values
- Switching color palettes — like using custom colors for discrete groups or gradients for continuous data
- Styling axes and legends — like adjusting tick marks, re-labeling categories, or modifying the legend’s appearance
- Transforming the data — like applying a log scale on the y-axis or aggregating values into bins
You don’t have to change the data to do any of this — it’s all handled by the scale_*()
functions.
Most scale_*()
functions have share some general arguments:
name
: set a custom axis or legend titlebreaks
: define steps to display along the axis or in the legendlabels
: customize the value text on the axis or in a legend
Oh no! 😱
It seems like you haven’t enrolled in the course yet!
Join many other students today and become the ggplot2 expert your company needs!
Or Login