ggplot2 is R's most powerful data visualization package, built on the Grammar of Graphics—a systematic framework that treats plots as compositions of independent layers (data, aesthetics, geometries, statistics, scales, coordinates, and facets). Originally created by Hadley Wickham, ggplot2 is now the industry standard for publication-quality graphics in R, deeply integrated into the tidyverse ecosystem. Unlike base R plotting, which describes what to draw, ggplot2 describes how variables map to visual properties, enabling you to build complex visualizations incrementally. The key mental model: every plot is a layered combination of components added with +, where swapping one layer (e.g., geom_point() for geom_line()) transforms the visualization without rewriting the entire plot. As of version 4.0.0 (2025), ggplot2 uses an S7 backend and introduces element_geom() for controlling geom appearance through the theme system.
What This Cheat Sheet Covers
This topic spans 21 focused tables and 202 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core Building Blocks
These are the seven layers (plus saving) that every ggplot2 plot is assembled from. Start with ggplot() and aes() to declare your data and mappings, then keep adding geoms, scales, coordinates, facets, and themes with + until the picture is right — understanding how these pieces fit together is the whole game.
| Component | Example | Description |
|---|---|---|
ggplot(data = df, aes(x, y)) | • Initializes a plot object • specifies data and global aesthetic mappings inherited by all layers. | |
aes(x = mpg, y = hp, color = cyl) | • Maps variables to aesthetics ( x, y, color, fill, size, shape, alpha, linetype)• placed in ggplot() for global or in geom_*() for layer-specific. | |
geom_point()geom_line() | • Defines the geometric object (visual representation) • each geom creates a new layer. | |
stat_summary(fun = mean) | • Applies statistical transformation before plotting • every geom has a default stat (e.g., geom_bar() uses stat_count()). | |
scale_x_continuous()scale_color_manual() | • Controls how data values map to visual properties • customizes axes, colors, sizes, limits, labels, and transformations. |