Grammar of Graphics in R

Learn about the structured approach followed in the ggplot2 package with the grammar of graphics to build visualizations.

What is the grammar of graphics?

First, let’s think about what a graphic is. How do we describe a graphic concisely, and can we build a representation of the description? The answers to these questions can help us understand the concepts involved in the grammar of graphics.

If we look at any graphic, we are likely to find a mapping of certain variables from the dataset to their visual representation in the graphic. The variables or data properties are typically numerical or categorical and are mapped visually as points (x and y coordinates), line colors, different markers, heights of bars, etc.

We know that the grammar for a language is a set of rules describing the correct and acceptable usage of words. These words can be combined in a predefined, logical way, to form meaningful sentences. Similarly, graphics grammar offers principles for arranging mathematical and aesthetic aspects into a meaningful graph, implying that graphics are constructed on an underlying grammar.

There are two important principles here:

  • Different layers of grammatical elements are used to create graphics.
  • Plots are built with appropriate aesthetic mappings to make them plots meaningful.

In short, the grammar states that a statistical graphic is a mapping from data to aesthetic qualities (color, shape, and size) of geometric objects (points, lines, bars). Additionally, the plot might include statistical data transformations depicted in a particular coordinate system.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy