Data and Aesthetic Mappings
You'll learn how to connect your dataset's variables to visual properties using ggplot2's aes() function—the bridge between raw data and what you see on screen.
The Core Idea
In the Grammar of Graphics (which ggplot2 implements), aesthetic mappings define how your data becomes visible. Think of aesthetics as the visual channels through which information flows: position (x, y), color, size, shape, transparency, and more. The aes() function is your declaration: "This variable controls that visual property."
How It Works
When you write aes(x = height, y = weight, color = gender), you're creating a mapping:
- The
heightcolumn controls horizontal position - The
weightcolumn controls vertical position - The
gendercolumn controls color
This is fundamentally different from setting a property. Mapping means the visual property varies with the data. If you have 100 rows, you get 100 points positioned and colored according to those mappings.
Common Aesthetic Properties
- Position:
x,y— where points/bars/lines appear - Color:
color(outlines),fill(interiors) - Size:
size— area or thickness - Shape:
shape— different point symbols - Transparency:
alpha— opacity level - Line type:
linetype— solid, dashed, dotted
The magic happens when ggplot2 automatically creates scales and legends based on your mappings. Map a categorical variable to color? You get discrete colors and a legend. Map a continuous variable? You get a gradient.
Key Takeaway: Aesthetic mappings in aes() translate data columns into visual properties, creating a systematic link between what your data contains and what your audience sees.