ggplot2 Cheat Sheet
Reference for ggplot2's grammar of graphics, common geoms, faceting, and theming used to build layered statistical visualizations in R.
2 PagesIntermediateFeb 28, 2026
Grammar of Graphics Basics
Map data to aesthetics and add a geometry layer.
r
library(ggplot2)ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) + geom_point(size = 2, alpha = 0.7) + labs(title = "Engine Size vs Highway MPG", x = "Displacement (L)", y = "Highway MPG") + theme_minimal()
Common Geoms
The most frequently used chart types.
r
# Bar chartggplot(mpg, aes(x = class)) + geom_bar()# Line chartggplot(economics, aes(x = date, y = unemploy)) + geom_line()# Boxplotggplot(mpg, aes(x = class, y = hwy)) + geom_boxplot()# Histogramggplot(mpg, aes(x = hwy)) + geom_histogram(binwidth = 2)# Smoothed trend lineggplot(mpg, aes(x = displ, y = hwy)) + geom_point() + geom_smooth(method = "lm", se = TRUE)
Faceting & Themes
Build small multiples and control non-data plot elements.
r
ggplot(mpg, aes(x = displ, y = hwy)) + geom_point() + facet_wrap(~ class, ncol = 3) + # small multiples by category theme_bw() + theme(legend.position = "bottom")ggsave("plot.png", width = 8, height = 5, dpi = 300)
Key Concepts
The layered vocabulary behind every ggplot2 chart.
- ggplot()- Initializes a plot object with a dataset and default aesthetic mappings
- aes()- Maps data columns to visual properties (x, y, color, fill, size, shape)
- geom_*- Geometric layer that determines the chart type: geom_point, geom_bar, geom_line, etc.
- facet_wrap/facet_grid- Splits the plot into a grid of subplots (small multiples) by a categorical variable
- scale_*- Controls axis/legend mapping, e.g. scale_color_manual() for custom colors
- theme()- Adjusts non-data plot elements: fonts, gridlines, legend position, background
- stat vs geom- Every geom has a default stat (e.g. geom_bar uses stat_count) that transforms data before drawing
Pro Tip
Build ggplot2 charts by adding layers incrementally with + and re-running after each addition - since the grammar of graphics is layer-based, this makes it easy to see exactly which layer introduced an unexpected change.
Was this cheat sheet helpful?
Explore Topics
#Ggplot2#Ggplot2CheatSheet#DataScience#Intermediate#GrammarOfGraphicsBasics#CommonGeoms#FacetingThemes#KeyConcepts#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance