Legends

As we mentioned in Chapter 3, The Layers and Grammar of Graphics, legends are guides that represent the inverse of the applied scales and are used to trace back the plot elements to the aesthetic mapping. For this reason, legends are defined in ggplot2 as guides since they represent guides to the aesthetic mapping. The legend that is created by ggplot2 depends on the aesthetic mapping and the geometry that is used in the plot. For instance, Figure 5.1 contains a continuous color scale, Figure 5.2 contains boxplots, and Figure 5.4 contains points.

Since legends are recreated by scale functions, most of the time you will need to use scale functions to modify the legend's appearance. In this section, we will go through how to change the four ...

Get ggplot2 Essentials 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.