22.1 Introduction

Definition of a grammar: “the fundamental principles or rules of an art or science” (OED Online 1989).

“In order to unlock the full power of ggplot2, you’ll need to master the underlying grammar. By understanding the grammar, and how its components fit together, you can create a wider range of visualizations, combine multiple sources of data, and customise to your heart’s content.”

“The next chapters discuss the components in more detail, and provide more examples of how you can use them in practice.”

Grammar versus chart heuristics. Often we match data type to a standard chart type (for example: bar chart for categorical comparisions).

4 parts of a Layer

  1. Data and aesthetic mapping. “Along with the data, we need a specification of which variables are mapped to which aesthetics.” (Wickham, 2010, p. 10)

  2. Stat. “A statistical transformation, or stat, transforms the data, typically by summarizing them in some manner…A statistical transformation, or stat, transforms the data, typically by summarizing them in some manner.” (Wickham, 2010, p. 10)

  3. Geom. “Geometric objects, or geoms for short, control the type of plot that you create. For example, using a point geom will create a scatterplot, whereas using a line geom will create a line plot.

We can classify geoms by their dimensionality:

• 0d: point, text, • 1d: path, line (ordered path), • 2d: polygon, interval.” (Wickham, 2010, p. 11)

  1. Position adjustment Examples include geom_jitter or how bar plots adjust so the lines do not overlap.

Review of key terms

Geom: point, bar, boxplot, line

Aesthetics: size, color, shape, position Aesthetics finder

Benefits of using the Grammar

  • Allows one to iterate in the creation and/or updating of a plot.
  • Gives a language for viewing, and learning from, existing data viz. 
  • Enables a better process by focusing the viz developer on the intended purpose of the visual/analysis (not just matching a chart to data).
  • Expands data viz beyond just how to use this particular software syntax.