1.3 How does ggplot2 fit in with other R graphics?

There are a number of other graphics systems available in R: base graphics, grid graphics and trellis/lattice graphics. How does ggplot2 differ from them?

  • Base graphics were written by Ross Ihaka based on experience implementing the S graphics driver and partly looking at Chambers et al.10 Base graphics has a pen on paper model: you can only draw on top of the plot, you cannot modify or delete existing content. There is no (user accessible) representation of the graphics, apart from their appearance on the screen. Base graphics includes both tools for drawing primitives and entire plots. Base graphics functions are generally fast, but have limited scope. If you’ve created a single scatterplot, or histogram, or a set of boxplots in the past, you’ve probably used base graphics.

  • The development of “grid” graphics, a much richer system of graphical primitives, started in 2000. Grid is developed by Paul Murrell, growing out of his PhD work.11 Grid grobs (graphical objects) can be represented independently of the plot and modified later. A system of viewports (each containing its own coordinate system) makes it easier to lay out complex graphics. Grid provides drawing primitives, but no tools for producing statistical graphics.

  • The lattice package, developed by Deepayan Sarkar, uses grid graphics to implement the trellis graphics system of Cleveland12 and is a considerable improvement over base graphics. You can easily produce conditioned plots and some plotting details (e.g., legends) are taken care of automatically. However, lattice graphics lacks a formal model, which can make it hard to extend. Lattice graphics are explained in depth in Deepayan Sarkar.13

  • ggplot2, started in 2005, is an attempt to take the good things about base and lattice graphics and improve on them with a strong underlying model which supports the production of any kind of statistical graphic, based on the principles outlined above. The solid underlying model of ggplot2 makes it easy to describe a wide range of graphics with a compact syntax, and independent components make extension easy. Like lattice, ggplot2 uses grid to draw the graphics, which means you can exercise much low-level control over the appearance of the plot.

  • htmlwidgets, http://www.htmlwidgets.org, provides a common framework for accessing web visualisation tools from R. Packages built on top of htmlwidgets include leaflet (https://rstudio.github.io/leaflet/, maps), dygraph (http://rstudio.github.io/dygraphs/, time series) and networkD3 (http://christophergandrud.github.io/networkD3/, networks).

  • plotly, https://plotly-r.com, is a popular javascript visualisation toolkit with an R interface. It’s a great tool if you want to make interactive graphics for HTML documents, and even comes with a ggplotly() function that can convert many ggplot2 graphics into their interactive equivalents.

Many other R packages, such as vcd,14 plotrix15 and gplots,16 implement specialist graphics, but no others provide a framework for producing statistical graphics. A comprehensive list of all graphical tools available in other packages can be found in the graphics task view at http://cran.r-project.org/web/views/Graphics.html.

  1. Graphical Methods for Data Analysis.↩︎

  2. Paul Murrell, “Investigations in Graphical Statistics” (PhD thesis, The University of Auckland, 1998).↩︎

  3. Visualizing Data.↩︎

  4. Lattice: Multivariate Data Visualization with R (Springer, 2008).↩︎

  5. David Meyer, Achim Zeileis, and Kurt Hornik, “The Strucplot Framework: Visualizing Multi-Way Contingency Tables with Vcd,” Journal of Statistical Software 17, no. 3 (2006): 1–48, http://www.jstatsoft.org/v17/i03/.↩︎

  6. Jim Lemon et al., Plotrix: Various Plotting Functions, 2008.↩︎

  7. Gregory Warnes, Gplots: Various R Programming Tools for Plotting Data, 2007.↩︎