5.1 Introduction
This chapter has two goals: we are going to learn to make graphs and figures that are good enough to submit to a scientific journal, using a contributed package called ggplot2
. And, we are going to fit General Linear Models with interaction terms, which allow the effect of one of your predictor variables to modify the effect of another one. It is not logically necessary to put these two topics into the same chapter, but it works fairly well, since looking at plots allows you to appreciate what a statistical interaction is; and because we will tackle both topics using the same dataset.
The ggplot2
package, created by Hadley Wickham, implements an idea called The Grammar of Graphics, in which graphical representations are built up progressively out of smaller elements, like words being chained together to make up sentences. ggplot
is part of the mega-package called tidyverse
, so as long as you have run library(tidyverse)
in the current session, you will have access to ggplot2
and all its functions.
When you plot with the ggplot()
function, you start by defining a canvas whose key dimensions and attributes map onto variables in the dataset. That mapping is called the aesthetic of the plot, controlled by the function aes()
.Then you add layers progressively to that canvas, notably geometric objects that represent the data (geom
s), thematic objects that modify the appearance, and other elements like text annotations. Your plot is itself an R object, which you can print to various output devices, notably the screen, or a picture writer (.jpeg or .png) or a PDF writer.