The Grammar of Graphics

Every time you draw a chart, you are actually doing many different things. You are:

  • Defining the data that will be shown to the user

  • Determining how to summarize or transform the data

  • Determining the graphical objects that will be used to represent the data

  • Determining how to divide the data, and how to show different partitions

  • Determining how the chart looks

When you draw a chart with most conventional tools (such as spreadsheets and presentation programs), you begin by picking a style of chart like a scatter plot, a pie chart, or a bar chart for your data. You may then refine the chart slightly by tweaking the size, color, and other visual parameters. These tools don’t reflect the thought process in drawing a chart. If you have to summarize your data before plotting (for example, when plotting a histogram), it can be awkward to do so. It is often hard to tweak how the results are displayed. Worst of all, it can be difficult to pick a different object to represent the data.

The grammar of graphics is designed to help separate each step of the charting process. This can help you decide the best way to visualize data, and is especially helpful for defining new types of plots. Each of these different aspects of the charting process is given a name in ggplot2; the tool reflects the language. The ggplot2 package includes a variety of functions for altering each component of a plot. (The qplot function above simplifies this process by allowing you to use arguments to ...

Get R in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.