Building Layered Plots

Layered plots are one way to achieve new insight and actionable intelligence when working with complex data.  ggplot is well suited for layered plots.

Data Pre-Processing

To make graphs with ggplot(), the data must be in a data frame and in “long” (as opposed to wide) format.  Converting between “wide” and “long” data formats is facilitated with the reshape2 package.  Specifically, the melt() function converts wide to long format, and the cast() function converts long to wide format.  The following code block presents examples of the two data formats.

Basic Plot Commands

The ggplot() function is used to create a plot.  It has two arguments: data and an aesthetic mapping, which is accomplished using aes().  There are 3 basic uses of the ggplot() function:

The first method is used if all plot layers rely on the same data and the same set of aesthetics.  The second method specifies the default data frame, but no aesthetics are defined upfront. This is useful when aesthetics are defined by layer and geometric objects (or geoms for short) are layered into the plot using the + operator.  The third method launches an empty graphic device, creating a skeleton ggplot object, which is made visible only after data and geoms are added.  The last method is preferred when creating complex plots that call multiple data frames to populate different plot layers.

ggplot() Examples

The grammar of graphics is shown in the following charts.  The plots depict the Doha Cloud Mask, or the percentage of sky covered with clouds, by month.  The examples are created using the three different methods to call to ggplot().  The first two charts rely on different data inputs and graphical aesthetics.  The final plot uses data inputs from multiple data frames and combines all the aesthetics and graphical layers from the first two plots.

Click to enlarge

Click to enlarge

Click to enlarge

Click to enlarge

Click to enlarge

Click to enlarge


The examples above are introductory in nature, showing that complex plots can be created with just a few lines of code. The default theme also pre-formats the plot size, plot background, axes spacing and font, quartile line colors, major and minor gridlines, and legend. Simplified syntax is confirmed.

The objective, of course, is to master low level control of all plot elements. To this end, the following sections serve as a reference, using basic data and structured examples.

Back | Next

Leave a Reply

Your email address will not be published. Required fields are marked *

two + 15 =