The grammar of graphics (the 'gg' in ggplot2)

The ggplot package in R is an implementation of Leland Wilkonson's Grammar of Graphics. I have here described real quickly this grammar, and listed the main functions. You can look at lab 2 to see how these are actually used in practice. This is provided more as a cheat sheet than a tutorial.

Visual Variables and aesthetics

The first part of the grammar is that there are “visual variables” we use to denote different data values. Common examples include the x and y axes, or the fill color of a polygon, or the border used for a polygon, or the size, or the alpha value of the ink density, etc. In the grammar of graphics, these are called “aesthetics”. There is only one function to set the aesthetics:

Geometries

Given the aesthetics, we will depict the various data using different “geometries.” Common geometries include points, lines, paths, polygons, histograms, boxplots, tiles, rasters, hexbins, etc. These will be created using functions like:

Each of these geometries will have x and y coordinates, but it may also have other aesthetics, such as: fill, border, shape, size, etc. Not all aesthetics are appropriate for all geometries. For example, while a polygon has both a border color and a fill color, a line can not.

Scales

Each aesthetic, or visual variable, has it's own scale. Are the axes scaled linearly? Are they categorical? What about the colors, how are they determined? ggplot sets these with scale functions, for example

Small Multiples (facets)

One of the most powerful visual tricks is to invite readers to make a comparison by showing side-by-side graphics of similar data. I used these, for example, to show the maps of climate data by year and season. Tufte calls these “small multiples.” The grammar of graphics calls these “facets,” (as if data are a complicated 3-D object, and you are only looking at one facet at a time). There are two main facet functions:

The first is a series of maps or graphs, possibly wrapping to a new line, and the second is a matrix of maps or graphs (for example, year only the horizontal direction and season along the vertical direction)

Guides (Legends)

There are a series of functions to manipulate legends. I haven't really explored these yet. I tend to use default legends. Every so often, I'll post-process the legends in something like Adobe Illustrator or Inkscape.