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 main aspects of legends: the title, labels, legend box, and legend position. But, first of all, let's see how we can remove the legend.
To remove the legend, you can use the guide argument of the scale function or directly use the guides()
function...