concept legend in category R

This is an excerpt from Manning's book Exploring Data with R MEAP V05.
Figure 3.18 Putting the legend to the right (this is the default placement).
![]()
Listing 3.18 Using `legend.position` argument of `theme()` to put the legend to the right of the plot.
```{r plot_legend_right} ggplot(dmd, aes(x = carats, y = price)) + geom_point(aes(shape = clarity)) + labs(shape = "Clarity") + theme(legend.position = "right") //#A ```
The legend can be put in other locations. The plotting code can be revised (Listing 3.19) to place the legend at the "bottom" of the plot (Figure 3.19).
Figure 3.19 Putting the legend at the bottom.
![]()
Listing 3.19 Using `legend.position` argument of `theme()` to put the legend below the plot.
```{r plot_legend_bottom} ggplot(dmd, aes(x = carats, y = price)) + geom_point(aes(shape = clarity)) + labs(shape = "Clarity") + theme(legend.position = "bottom") //#A ```By using "bottom" instead of "right" for legend.position, we see that the legend is at the bottom. We can also use the values "top" and "left" to place the legend at the top or to the left.
Using the legend.justification argument in theme() (Listing 3.20) we can have the legend vertically justified to the "top" of the plot (Figure 3.20).
Figure 3.20 Default legend position to the right but justified to the top.
![]()
Listing 3.20 Using `legend.justification` argument of `theme()` to justify the legend toward the top of the visualization.
```{r plot_legend_just_top} ggplot(dmd, aes(x = carats, y = price)) + geom_point(aes(shape = clarity)) + labs(shape = "Clarity") + theme(legend.justification = "top") //#A ```

This is an excerpt from Manning's book R in Action, Second Edition: Data analysis and graphics with R.
When more than one set of data or group is incorporated into a graph, a legend can help you to identify what’s being represented by each bar, pie slice, or line. A legend can be added (not surprisingly) with the legend() function. The format is
Again, not all functions allow you to add these options. See the help for the function of interest to see what options are accepted. For finer control and for modularization, you can use the functions described in the remainder of this section to control titles, axes, legends, and text annotations.