The Base Layer: ggplot() and Data Mapping
You'll learn how to create the foundation of any ggplot visualization by initializing the plot object and mapping your data to visual properties.
What Is the Base Layer?
Every ggplot visualization starts with a base layer created by the ggplot() function. Think of this like preparing a canvas before painting: you're not drawing anything visible yet, but you're setting up what data you'll visualize and how variables in that data correspond to visual elements on the plot.
The ggplot() function takes two key components:
- Data: The dataset you want to visualize (typically a data frame)
- Aesthetic mappings: Defined using
aes(), these map variables in your data to visual properties like x-position, y-position, color, size, or shape
How It Works
When you call ggplot(), you're creating a plotting object that "knows" about your data and how to interpret it, but doesn't yet display anything. The aesthetic mappings tell ggplot which columns should control which visual aspects.
# Basic structure - creates foundation but shows nothing yet
ggplot(data = my_data, mapping = aes(x = variable1, y = variable2))
This establishes the coordinate system and data bindings. You're essentially saying: "When I add visual elements later, use variable1 for horizontal position and variable2 for vertical position."
The Analogy
Imagine you're organizing a seating chart. The ggplot() call is like declaring "We have these guests (data) and each person's age determines their table (x-axis) while their department determines their seat number (y-axis)." You haven't placed anyone yet, but the rules are set.
Key Takeaway: The ggplot() function with aes() creates the foundational layer by linking your data to visual properties—it's the essential first step before adding any geometric objects.