Graphics Reference
In-Depth Information
AppendixA.Introduction to ggplot2
Most of the recipes in this topic involve the ggplot2 package, written by Hadley Wickham. gg-
plot2 has only been around for a few years, but in that short time it has attracted many users in
the R community because of its versatility, clear and consistent interface, and beautiful output.
ggplot2 takes a different approach to graphics than other graphing packages in R. It gets its name
from Leland Wilkinson's grammarofgraphics, which provides a formal, structured perspective
on how to describe data graphics.
Even though this topics deals largely with ggplot2, I don't mean to say that it's the be-all and end-
all of graphics in R. For example, I sometimes find it faster and easier to inspect and explore data
with R's base graphics, especially when the data isn't already structured properly for use with
ggplot2. There are some things that ggplot2 can't do, or can't do as well as other graphing pack-
ages. There are other things that ggplot2 can do, but that specialized packages are better suited
to handling. For most purposes, though, I believe that ggplot2 gives the best return on time in-
vested, and it provides beautiful, publication-ready results.
Another excellent package for general-purpose graphs is lattice, by Deepyan Sarkar, which is an
implementation of trellisgraphics. It is included as part of the base installation of R.
If you want a deeper understanding of ggplot2, read on!
Background
In a data graphic, there is a mapping (or correspondence) from properties of the data to visual
properties in the graphic. The data properties are typically numerical or categorical values, while
the visual properties include the xand ypositions of points, colors of lines, heights of bars, and
so on. A data visualization that didn't map the data to visual properties wouldn't be a data visu-
alization. On the surface, representing a number with an xcoordinate may seem very different
from representing a number with a color of a point, but at an abstract level, they are the same.
Everyone who has made data graphics has at least an implicit understanding of this. For most of
us, that's where our understanding remains.
In the grammar of graphics, this deep similarity is not just recognized, but made central. In R's
base graphics functions, each mapping of data properties to visual properties is its own special
case, and changing the mappings may require restructuring your data, issuing completely differ-
ent graphing commands, or both.
To illustrate, I'll show a graph made from the simpledat data set from the gcookbook package:
Search WWH ::




Custom Search