Comparison of Graphic Tools
When it comes to producing graphics in R, there are basically three choices:
- Base graphics which ships with R,
- the lattice package extension, and
- the ggplot2 package extension.
Base graphics was described extensively in the previous few chapters, and is the preferred choice for creating highly customized charts, like the polar windrose plot below, where flexibility and control over all graph objects is essential:
Unfortunately, the code for base graphics is cumbersome and often times challenging.
Both lattice and ggplot2 make creating plots with multivariate data much easier. Of the two, I find lattice is the most demanding, and not to my liking. Its a matter of personal choice, but one fact is clear: ggplot2 has simplified code syntax. The package has wrappers around the base syntax that eliminates the hassle of managing many repetitive features, like custom legends. True to the author’s goal, “ggplot2 takes the good parts of base and lattice graphics and none of the bad parts.” Finally, ggplot2 relies on a grammar of graphics (hence gg-plot) that simplifies making complex, multi-layered visualizations, like the one below:
You’ll need the most recent version of R to install the most recent version of ggplot2.
The following books represent primary source material used in this tutorial:
- Leland Wilkison’s Grammar of Graphics (2005);
- Hadley Wickham’s ggplot2: Elegant Graphics for Data Analysis (2009); and
- Winston Chang’s R Graphics Cookbook (2013).
Defining the Grammar of Graphics
Wilkinson (2005) created the grammar of graphics to describe the essential features that underlie all statistical graphs. The layered grammar of graphics for R was developed by Wickham (2005). In brief, the grammar reduces a statistical graph to a simple mapping: from data to geometric objects (points, lines or bars) with aesthetic attributes (color, shape, and size). The plot may also contain statistical transformations of data and is drawn onto a coordinate system. Faceting extends the basic grammar to include multiple plots or window panes based on data subsets. It is the combination and layering of these components that define the grammar. The grammar as implemented by ggplot2 exploits the low-level graphical object controls intrinsic to R while using a simplified code syntax.