Welcome to a ggplot2 grammar of guide! This guide isn’t comprehensive, and it will likely continue to evolve. But it’s at a place that it might be useful to orient you, a colleague, or a student to ggplot2 and extensions.
ggplot2 is an open source software package and implementation of the ‘Grammar of Graphics’ framework. I’m grateful to the package creator Hadley Wickham and the ggplot2 team that has worked so hard to creating this incredible tool, to the contributors and maintainers of the R language in which it is built, and to folks who have given me valuable feedback on communicating data viz and data viz tools, especially my students!
Right now, you’re actually on the visual table of contents page for quick reference.
But you can get started with the guide itself here, which contains a guided discussion for thinking about data viz and ggplot2, and code+output walk-throughs for getting the work done.
first grammar lesson: A data visualization, is composed of geometric shapes, that take on aesthetics which represent variables from a data set. ggplot() + aes() + geom_*()
To use the visual table of contents, mouse over to see a preview of what will be covered, then click any link or visual preview to go through to the flipbook (code-movies built with the flipbookr and xaringan packages) that will show you a detailed build of the previewed contents.
Not into human language grammar? Just ignore that first column!
What? | How? | ||
---|---|---|---|
1. The Declarative Mood | Declaring the data | ggplot(data = gapminder) + | |
2. The Interogative Mood | Asking for representation of variables by aesthetics (color, size, x position, etc.) (also known as aesthetic mapping) | aes(color = pop) + | |
- Modifiers I | modifying default aesthetic labels (and plot labels) | labs(color = “Continent”) + labs(title = “my title”) + |
|
- Modifiers II | modifying the default coordinate system (how the positional aesthetics appear - x and y) | coord_polar() + | |
- Modifiers III | modifying default aesthetic scales | scale_color_viridis_d() + | |
3. Nouns | geometric layers taking on the aesthetics representing variables | see below table | geom_point() + |
4.The Conditional Mood | Making local, geom-specific declarations rather than global declarations | geom_point( | |
data is geom-specific |
data = gapminder, | ||
aesthetic representation is geom-specific |
aes(size = population), | ||
aesthetics not representing variables; unmapped aesthetics (i.e. The Imperative Mood) | color = “blue” |
||
) + | |||
5. Interjections | Adding context with annotation layers | annotate(geom = “point”, x = 10, y = 12, color = “blue”) |
|
6. Punctuation | faceting breaks a plot into small plots (or “small multiples”) based on categorical variables | facet_wrap(~continent) | |
7. Greetings | themes changing plot look and feel | theme_minimal() | |
8. The Written Language | save plots with different resolutions and file formats | ggsave(file = “plot.png”, plot = g, height = “4in”, width = “6in”) |
|
9. Composition | Composing plots into ensembles | library(patchwork) (g1 + g2) | g3 library(cowplot) plotgrid(…,) |
|
10. Concision | We’ve been intentionally verbose, but there are strategies for being concise | last_plot(), switch out data with %+% , writing functions |
Above, the geom topics are intentionally cursory. There are tons of geoms – like nouns – and they can get distracting I find them to not be very grammatical – like nouns – and earlier we wanted to focus on grammar. But in the end, geoms are pretty fun - and there are a lot of them! Below gives you a preview of geometric marks used to describe continuous distributions, continuous-continuous joint distributions, visualizations of single series, of multiple series and discrete-discrete distributions. In the area of geoms, there have been many contributors
External Theme Packages
Spatial with geom_sf, tmap, and rayshader (topography & 3D ggplots)
Network visualization {ggraph} and {tidygraph}
Animation with {gganimate} (No hyperlinks currently on gifs. Click gganimate link to the left)
There’s not as much as I’d like here. Some gganimate <-> flipbookr struggle. But perhaps still useful as a starting point.
ggplot2 is very flexible, so there often there are more than one way of getting things done — which also means there is more than one way to introduce ggplot2. If you aren’t on board with this guide (and even if you are) you may want check out other resouces — I’ve learned a so much from them! The definitive and comprehensive guide to ggplot2 is the 3rd edition of ggplot2: elegant graphics for data analysis. Some other workshop materials are Alison Hill’s Take a Sad Plot and Make it Better, Garrick Aden-Buie’s A Gentle Guide to the Grammar of Graphics with ggplot2, Will Chase’s R you ready to make charts?, Malcom Barrett’s Designing ggplots making clear figures that communicate, and Thomas Lin Pederson’s Drawing anything with ggplot2. The ggplot2 cheatsheet is also excellent.