Moving Outside the Box for Data Visualization
By: Ryan Harrington
Tags: Data Visualization

In the graphs above, we see many elements that are common to graphs that we traditionally think of. There are bars. There are lines. There are points. However, none of the graphs themselves fit neatly into the box of a “bar chart”, a “line chart”, or a “scatterplot”. Each graph is something more than one of the boxes we might try to prescribe them.

In order to build complex visualizations like these and truly unleash our data storytelling capabilities, we must rethink how we traditionally build graphs. We need to consider a new framework that moves beyond traditional graphing conventions. We call this framework a “grammar of graphics”.

One of the most popular implementations of the “grammar of graphics” framework is Hadley Wickham’s¹ ggplot2 ² in R . He succinctly describes what a “grammar of graphics” is in his introduction to ggplot2 :

A grammar of graphics is a tool that enables us to concisely describe the components of a graphic. Such a grammar allows us to move beyond named graphics (e.g., the “scatterplot’’) and gain insight into the deep structure that underlies statistical graphics.

The key point that differentiates the “grammar of graphics” framework from how we traditionally think about graphs is that it operates by using layers. In ggplot2 , each layer is then further made up of 5 components. Each of these components are:

data — what data is being used to build the layer?

— what data is being used to build the layer? mapping — what field(s) from the data are being used to build the layer?

— what field(s) from the data are being used to build the layer? geometry — what shape should our data take when building the layer?

— what shape should our data take when building the layer? statistic — how should our data be transformed when building the layer?

— how should our data be transformed when building the layer? position — where should our data be placed when building the layer?

When building data visualizations using ggplot2 , we do not necessarily need to think about each of these components for every new layer that we build. ggplot2 will take care of much of that work behind the scenes for us. In most cases, you will simply need to consider the “data”, “mapping”, and “geometry” components of any given layer that you build. As you first begin working with the ggplot2 framework, the “statistic” and “position” components will (largely) stay working behind the scenes.

Here’s a toy example to consider:

This is a (very) made up dataset about clothing purchases that consists of 4 fields: Date , Item , Quantity , and Price . From the data, specific fields can be mapped to different characteristics of a given geometry. For example, in this case I would want to understand how the total quantity of each item purchased compares to each other. A great way to do this is by using a bar geometry. To create a bar graph using ggplot2 ³, I am required to use an x aesthetic and a y aesthetic. There are other aesthetics available for me to use, though I am not required to do so. The code that I would use to build this plot (after a bit of data manipulation) would look similar to:

Full code for generating and executing this Gist available on Github

My visualization would look like:

This is an extremely simple example that relies upon using one layer and one geometry that, ultimately, produces one of the more common types of graphs — a bar chart. To get a better sense of how more complex visualizations are built, look at the work done by Gina Reynolds in creating the “ggplot flipbook”. Below, you can see some of the examples that she built and how the different layers work together to bring a plot to life, including some of the visualizations used earlier in this article.

From the ggplot flipbook