ggplot2: Elegant Graphics for Data Analysis by WICKHAM, H.
Leland Wilkinson · Biometrics · 2011
The Grammar of Graphics (Wilkinson, 2005) introduced a new model for understanding statistical charts and graphics. Instead of relying on chart-type descriptors (bar chart, pie chart, scatterplot, SPLOM, Trellis, sparklines, small multiples, etc.) this model (called GoG) rests on a collection of algebraic and geometric specifications of the underlying structure of graphics. This collection consists of seven orthogonal classes. The term orthogonal means that each class contains one or more methods (functions) as elements, and all tuples in the sevenfold product of these sets of functions produce meaningful graphs. A consequence of this orthogonality is a high degree of expressiveness: we can produce a huge variety of graphical forms or chart types in such a system. It is claimed that virtually all known statistical charts, and perhaps a great number of meaningful but undiscovered charts, can be generated by this relatively parsimonious system. A second claim of GoG is that this system describes the meaning of what we do when we construct statistical graphics. It is more than a taxonomy. It is a computational system based on the underlying mathematics of representing statistical functions of data. As such, GoG was designed to serve as a foundation for producing, reading, and understanding statistical graphics. There have been several implementations of GoG since the first edition of the book came out in 1999. A team at SPSS/IBM developed a system called nViZn that included all seven GoG classes. The nViZn platform included a parser for Dan Rope's Graphics Production Language shown in the book, as well as an XML GoG specification designed by Graham Wills. A doctoral dissertation by Chris Stolte at Stanford led to the development of a GoG model for relational data (Stolte, Tang, and Hanrahan, 2002) that has developed into a rapidly growing company called Tableau (http://www.tableausoftware.com). Hadley Wickham is the author of the third major GoG implementation. This open-source project began as a dissertation at Iowa State. Wickham's book describes the project and serves as a user manual for the ggplot2 package. Wickham clearly did not sit down and program chapter-by-chapter from Wilkinson's book. Instead, he organized the ideas in a coherent framework that will be readily understandable to R users. As such, he has provided a valuable alternative to the standard R graphics and the Lattice package (Murrell, 2005; Sarkar, 2008). The graphics in the book are clean and lovely (with the exception of a few oddly scaled maps). The presentation is somewhat terse but quite understandable. Rather than summarize the chapters, which lay out the basics of the system, I will present ggplot2 specifications for a few well-known charts. Ordinarily this would be stuff only programmers would care about, but I think the elegance of Wickham's system is most clearly expressed in his language. Wickham offers a single function called qplot that produces many different charts in one line of code. For a cars_data file, a scatterplot of mileage by displacement colored by a categorical variable denoting number of cylinders, for example, is produced by the command: But the real power and simplicity of the system is exposed in the ggplot function. Here is the same graphic in that specification: We used two lines to accomplish what we did previously in one. Now, however, we can expand this specification into a variety of charts with only slight alterations. The first line specifies a frame for the graphic based on the values of mileage and displacement. (A frame in GoG is a region bounding the plotting area, the product of the range and domain, usually delineated by axes.) The second line adds points to the frame and colors them by the number of cylinders. Want a loess smoother in the scatterplot? Add Want to facet (panel) by displacement and weight? Add All these commands can be put on one line connected with the plus signs, but the multiline layout makes clear the geometric components (geoms) that are being added to the graph. It is always simpler to make a command (like qplot) that produces a specific type of graphic. With GoG, however, expressiveness is achieved by making the simple things slightly more complicated and the complicated things much simpler. I would guess that the inventory of functions in ggplot2 is smaller than those of the other R graphics packages and especially of other statistics packages. And I would guess that this system produces more different types of charts than its competitors. There are minor defects in the book that do not detract from its usefulness enough to mention. Overall, I would have wished for more examples and a comprehensive list of options and definitions. That kind of timely material is usually presented on the Web nowadays, however (see http://had.co.nz/ggplot2/). This book provides enough of an introduction to get one going with ggplot2, however. Consequently, I highly recommend it. Even though Web readers are ubiquitous, this book weighs a bit less than an iPad. And its batteries last forever.