🎨 ggplot2 Fundamentals: Painting Pictures with Data
Imagine you’re an artist with a magical paintbrush. Instead of colors, you use data. Instead of a canvas, you use a screen. Welcome to ggplot2—the artist’s toolkit for data!
🖼️ The Big Picture: What is ggplot2?
Think of ggplot2 like building with LEGO blocks. Each block is a layer. You stack them one by one to create something amazing!
The Magic Formula:
ggplot(data) +
aesthetics +
shapes +
style
It’s like making a sandwich:
- 🍞 Data = the bread (foundation)
- 🥬 Aesthetics = what goes where (lettuce here, tomato there)
- 🧀 Geoms = the shapes (circles, lines, bars)
- 🎨 Themes = how it looks (fancy or simple)
📍 ggplot Basics: Starting Your Masterpiece
Every great painting starts with a blank canvas. In ggplot2, we create our canvas like this:
library(ggplot2)
ggplot(data = mydata)
That’s it! You just made a blank canvas. Nothing shows up yet because we haven’t told it WHAT to draw or HOW to draw it.
Real Example:
# We have car data
ggplot(data = mtcars)
Think of this like saying: “I want to paint something about cars.” But you haven’t picked up your brush yet!
The Plus Sign (+) is Your Best Friend
In ggplot2, we ADD layers with +. It’s like stacking pancakes!
ggplot(data = mtcars) +
aes(x = mpg, y = hp) +
geom_point()
Each + adds another layer to your picture.
🎯 Aesthetic Mappings: Telling Data Where to Go
Aesthetics answer the question: “Which data goes where?”
Imagine you’re sorting toys into boxes:
- Red toys → Red box
- Big toys → Big shelf
- Round toys → Round basket
That’s what aes() does—it sorts your data!
The Basic Aesthetics
| Aesthetic | What It Controls | Example |
|---|---|---|
x |
Left-right position | aes(x = age) |
y |
Up-down position | aes(y = height) |
color |
The color | aes(color = gender) |
size |
How big | aes(size = weight) |
shape |
What shape | aes(shape = type) |
Example: Making a Scatter Plot
ggplot(mtcars) +
aes(x = wt, y = mpg) +
geom_point()
This says:
- x-axis = car weight
- y-axis = miles per gallon
- Draw = dots
Adding Color Magic
ggplot(mtcars) +
aes(x = wt,
y = mpg,
color = factor(cyl)) +
geom_point()
Now each dot is colored by how many cylinders the car has. 4-cylinder cars might be red, 6-cylinder blue, 8-cylinder green!
Inside vs. Outside aes()
This is SUPER important:
# INSIDE aes() = varies by data
aes(color = species)
# OUTSIDE aes() = same for all
geom_point(color = "blue")
Think of it like:
- Inside aes(): “Color each point based on what team they’re on”
- Outside aes(): “Make ALL points blue, no exceptions”
🔷 Geometric Objects: The Shapes of Your Story
Geoms are the actual shapes you see. They’re like different stamp shapes:
graph TD A["geom_point"] --> B["Dots/Scatter"] C["geom_line"] --> D["Connected Lines"] E["geom_bar"] --> F["Bar Charts"] G["geom_histogram"] --> H["Distribution Bars"] I["geom_boxplot"] --> J["Box & Whiskers"]
The Most Popular Geoms
1. Points (Scatter plots)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
Use when: Comparing two numbers
2. Lines
ggplot(economics, aes(x = date, y = pop)) +
geom_line()
Use when: Showing change over time
3. Bars
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar()
Use when: Counting categories
4. Histogram
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(bins = 10)
Use when: Seeing distribution of one number
5. Boxplot
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_boxplot()
Use when: Comparing distributions across groups
Stacking Multiple Geoms
The coolest part? You can stack geoms like LEGO!
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth()
This draws dots AND adds a smooth trend line. Two layers, one picture!
📊 Faceting: Small Multiples Magic
Faceting is like having multiple small TVs instead of one big one. Each TV shows a different channel!
facet_wrap() — Wrapping Around
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
facet_wrap(~cyl)
This creates ONE plot for each cylinder value:
- One plot for 4-cylinder cars
- One plot for 6-cylinder cars
- One plot for 8-cylinder cars
The ~ means “split by”—like saying “one plot PER cylinder type.”
facet_grid() — Making a Grid
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
facet_grid(am ~ cyl)
This makes a GRID:
- Rows = automatic vs manual transmission
- Columns = 4, 6, or 8 cylinders
graph TD subgraph Facet Grid A["Row: Manual"] B["Row: Automatic"] C["Col: 4cyl"] D["Col: 6cyl"] E["Col: 8cyl"] end
Controlling Scales
# Same scale for all (default)
facet_wrap(~cyl)
# Free scales (each plot has its own)
facet_wrap(~cyl, scales = "free")
🎨 ggplot Themes: Dressing Up Your Plot
Themes are like outfits for your plot. The data stays the same, but the LOOK changes!
Built-in Themes
# Default gray
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_gray()
# Clean and minimal
theme_minimal()
# Classic look (white background)
theme_classic()
# Black and white
theme_bw()
# Dark theme
theme_dark()
Customizing with theme()
Want to change specific things? Use theme():
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme(
plot.title = element_text(size = 20),
axis.text = element_text(color = "blue"),
panel.background = element_rect(fill = "white")
)
Common Theme Elements
| Element | What It Changes |
|---|---|
plot.title |
Main title style |
axis.title |
Axis label style |
axis.text |
Numbers on axis |
legend.position |
Where legend goes |
panel.grid |
Grid lines |
panel.background |
Plot background |
Quick Legend Tricks
# Move legend to bottom
theme(legend.position = "bottom")
# Remove legend completely
theme(legend.position = "none")
# Put legend inside the plot
theme(legend.position = c(0.9, 0.9))
📐 ggplot Coordinates: Controlling the View
Coordinates control HOW you see your plot—like adjusting a camera!
Zooming In
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
coord_cartesian(xlim = c(2, 4), ylim = c(15, 25))
This zooms into a specific area WITHOUT removing data.
Flipping Axes
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar() +
coord_flip()
Turns vertical bars into horizontal bars!
graph LR A["Vertical Bar Chart"] -->|coord_flip| B["Horizontal Bar Chart"]
Fixed Aspect Ratio
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
coord_fixed(ratio = 1)
Makes sure 1 unit on x = 1 unit on y. Great for maps or when proportions matter!
Polar Coordinates (Pie Charts!)
ggplot(mtcars, aes(x = "", fill = factor(cyl))) +
geom_bar() +
coord_polar("y")
This bends a bar chart into a circle—a pie chart!
xlim and ylim Differences
Two ways to set axis limits:
# Method 1: coord_cartesian (KEEPS all data)
coord_cartesian(xlim = c(10, 30))
# Method 2: scale limits (REMOVES data outside)
scale_x_continuous(limits = c(10, 30))
Use coord_cartesian() when you want to ZOOM. Use scale limits when you want to FILTER.
🚀 Putting It All Together
Here’s a complete example using EVERYTHING:
library(ggplot2)
ggplot(data = mtcars) +
aes(x = wt,
y = mpg,
color = factor(cyl),
size = hp) +
geom_point(alpha = 0.7) +
facet_wrap(~am, labeller = labeller(
am = c("0" = "Automatic", "1" = "Manual")
)) +
theme_minimal() +
theme(legend.position = "bottom") +
coord_cartesian(ylim = c(10, 35)) +
labs(
title = "Car Performance",
x = "Weight (1000 lbs)",
y = "Miles Per Gallon",
color = "Cylinders",
size = "Horsepower"
)
What this does:
- 📊 Data: Uses mtcars dataset
- 🎯 Aesthetics: Weight on x, MPG on y, color by cylinders, size by horsepower
- 🔷 Geom: Draws semi-transparent points
- 📐 Facet: Splits by transmission type
- 🎨 Theme: Minimal style with bottom legend
- 📍 Coordinates: Zooms to MPG 10-35
- 🏷️ Labels: Nice titles for everything
🎓 Key Takeaways
- Start simple:
ggplot(data)+aes()+geom_*() - Layer by layer: Add complexity with
+ - Aesthetics inside aes(): For data-driven properties
- Choose the right geom: Points, lines, bars—match your question
- Facet for comparison: Small multiples reveal patterns
- Theme for polish: Make it beautiful
- Coordinates for control: Zoom, flip, transform
🌟 Remember: ggplot2 is like a language. The more you practice, the more fluent you become. Start with simple plots, then add layers one at a time. Before you know it, you’ll be creating data art!
The Grammar of Graphics in One Sentence:
Take some DATA, map it to AESTHETICS, draw it with GEOMS, maybe FACET it, style it with a THEME, and adjust the COORDINATES.
That’s it. You’re now a ggplot2 artist! 🎨
