ggplot2 is R's powerful visualization system based on the Grammar of Graphics. Create elegant, customizable plots with layered components.
Data Visualization in R with ggplot2
1. ggplot2 Basics
The foundation of every ggplot2 visualization:
library(ggplot2)
library(palmerpenguins) # Sample dataset
# Basic template:
# ggplot(data = <DATA>) +
# <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
# Scatterplot example
ggplot(data = penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g))
# Essential components:
# 1. Data (data frame)
# 2. Aesthetics (aes() - how vars map to visual properties)
# 3. Geometric objects (geoms - the shapes to draw)
ggplot2 Basics Quiz
Which function defines how variables map to visual properties?
2. Common Plot Types
Essential geoms for different data relationships:
# Scatterplot (numeric vs numeric)
ggplot(penguins) +
geom_point(aes(x = bill_length_mm, y = bill_depth_mm))
# Bar plot (categorical counts)
ggplot(penguins) +
geom_bar(aes(x = species))
# Histogram (numeric distribution)
ggplot(penguins) +
geom_histogram(aes(x = body_mass_g), bins = 30)
# Boxplot (distribution by category)
ggplot(penguins) +
geom_boxplot(aes(x = species, y = body_mass_g))
# Line plot (trends over time)
economics |>
ggplot(aes(x = date, y = unemploy)) +
geom_line()
Plot Types Quiz
Which geom would you use to show distributions by category?
3. Aesthetics and Customization
Enhance plots with colors, facets, and themes:
# Color by category
ggplot(penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species))
# Faceting (small multiples)
ggplot(penguins) +
geom_point(aes(x = bill_length_mm, y = bill_depth_mm)) +
facet_wrap(~species)
# Themes and labels
ggplot(penguins) +
geom_bar(aes(x = island, fill = species)) +
labs(title = "Penguins by Island",
x = "Island",
y = "Count") +
theme_minimal() +
theme(legend.position = "bottom")
# Manual scales
ggplot(penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
scale_color_manual(values = c("darkorange", "purple", "cyan4"))
Customization Quiz
How would you create separate plots for each species?
4. Statistical Transformations
Automated statistical computations in plots:
# Smooth trend lines
ggplot(penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g)) +
geom_smooth(aes(x = flipper_length_mm, y = body_mass_g))
# Bar plot with pre-computed stats
ggplot(penguins) +
geom_bar(aes(x = species, y = after_stat(prop), group = 1))
# Density plots
ggplot(penguins) +
geom_density(aes(x = body_mass_g, fill = species), alpha = 0.5)
# Quantile regression
ggplot(penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g)) +
geom_quantile(aes(x = flipper_length_mm, y = body_mass_g))
Stats Quiz
Which geom adds a smoothed conditional mean line?
5. Advanced Techniques
Professional-grade visualizations:
# Combining geoms
ggplot(penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
geom_smooth(aes(x = flipper_length_mm, y = body_mass_g))
# Annotations
ggplot(penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g)) +
annotate("text", x = 200, y = 4000, label = "Outliers") +
annotate("rect", xmin = 220, xmax = 230, ymin = 3000, ymax = 4000, alpha = 0.2)
# Coordinate systems
ggplot(penguins) +
geom_bar(aes(x = species, fill = species)) +
coord_flip()
# Saving plots
ggsave("penguin_plot.png", width = 8, height = 6, dpi = 300)
Advanced Quiz
How would you add text labels to specific plot locations?
6. ggplot2 Extensions
Enhance ggplot2 with specialized packages:
# Patchwork - plot composition
library(patchwork)
p1 <- ggplot(penguins) + geom_point(aes(x = bill_length_mm, y = bill_depth_mm))
p2 <- ggplot(penguins) + geom_boxplot(aes(x = species, y = body_mass_g))
p1 + p2 # Side by side
# gganimate - animated plots
library(gganimate)
ggplot(penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
transition_states(year)
# ggrepel - non-overlapping labels
library(ggrepel)
ggplot(penguins) +
geom_point(aes(x = flipper_length_mm, y = body_mass_g)) +
geom_text_repel(aes(x = flipper_length_mm, y = body_mass_g, label = island))
Extensions Quiz
Which package combines multiple ggplot2 plots into one?
×