This section is still being developed. The contents are subject to change.
ggplot2 syntax
“making infinite use of finite means” (Wilhelm von Humboldt)
Grammar is often defined as the system of rules for any given language and includes word meanings, internal structures, and word arrangement. Syntax is the form, structure and order used to construct statements. ggplot2’s syntax is built on the grammar & syntax of R. In R, objects are like nouns, and functions are like verbs (i.e., functions “do things” to objects).
ggplot2 syntax is built on layers, which we can use as templates to build increasingly customized and complex graphs.
The Data Layer
The data layer consists of a rectangular object (like a spreadsheet) with columns and rows. Every dataset in this book is simulated, so we’ll create one here: 150 home PCs sold in 1998, with each PC’s brand, operating system (os), processor speed (cpu_mhz), and price (price_usd). Faster processors cost more, and Windows 98 adds a little to the price.
Graphs have been categorized into the following types (you can see them in the floating table of contents to your left).
Univariate: if you have a single column you’re trying to visualize
Amounts: counts or simple summary statistics of one or two columns in a dataset
Proportions: ratio displays of part-to-whole
Distributions: displaying the shape of a variable’s values (normality, skewness, kurtosis, etc.)
Dates & Times: changes in quantitative or categorical variables over a dimension of time (date-time, date, year, month, quarter, etc.).
Relationships: how does the change of values in variable x affect the values in variable y (and possibly z)?
Some graphs can justifiably belong to more than one category. Wherever this is the case, I’ve tried to include links to other applications in the notes.
Assumptions
I’ve tried to write code so it can be read and understood without executing it.1 However, the examples assume readers can investigate R objects and answer questions related to their structure.2
Style & structure
Each graph has the following sections:
Theme
The color palette and font for the chapter’s graphs, and the piece of 90s pop culture they’re modeled on.
Requirements
What this graph requires, and a preview of the graph.
Description
A brief summary of the graph’s background, purpose, and aesthetics.
Set up
Packages
Code for installing development version of packages (if necessary) for graphs.
Data
The code that simulates the data for the graph (with set.seed(), so you get the same values every time).
Distribution
How the distribution we simulated from shapes the graph, in four steps:
COUNT IT: look at a handful of values and a few summary numbers.
DRAW IT: draw the data as a bar model or annotated graph.
NAME IT: write down the distribution and connect each parameter to the graph.
CHANGE IT: change the data (a smaller sample, a new group, a different distribution) and redraw the same graph to show something unexpected.
Grammar
Code
The steps for building the graph, followed by the code.
Graph labels have the labs_ prefix
Graph layers have a ggp2_ prefix
Graph
The finished graph.
More info (in some chapters)
Additional methods, arguments, or applications
Color palettes
Complementary packages
The diagram below shows how the sections fit together. The data is simulated in Set up, explained and then changed in Distribution, and graphed in Grammar.
I’ve attempted to balance brevity and clarity, but with the assumption that it’s best to err on the latter. I’ve also followed the general principle that if a graph can be easily built using one of ggplot2’s geom_* functions, that method is shown first.
The first section (Univariate) starts with bar graphs, which count how often each category appears in a single column.
This field manual follows The Rule of Least Power, in the sense that “a language with a straightforward syntax may be easier to analyze than an otherwise equivalent one with more complex structure.”↩︎
If not, I highly recommend the skimr and inspectdf packages for getting to know your data better.↩︎