Colour, Size, Shape and Other Aesthetic Attributes
To add additional variables to a plot, we can use other aesthetics like colour,
shape, and size (NB: while I use British spelling throughout this book, ggplot2
also accepts American spellings). These work in the same way as the x and y
aesthetics, and are added into the call to aes():
• aes(displ, hwy, colour = class)
• aes(displ, hwy, shape = drv)
• aes(displ, hwy, size = cyl)
ggplot2 takes care of the details of converting data (e.g., ‘f’, ‘r’, ‘4’) into
aesthetics (e.g., ‘red’, ‘yellow’, ‘green’) with a scale. There is one scale for
each aesthetic mapping in a plot. The scale is also responsible for creating a
guide, an axis or legend, that allows you to read the plot, converting aesthetic
values back into data values. For now, we’ll stick with the default scales
provided by ggplot2.