Graphics Reference
In-Depth Information
Sometimes a legend is redundant, or it is supplied in another graph that will be displayed with
the current one. In these cases, it can be useful to remove the legend from a graph.
In the example used here, the colors provide the same information that is on the x-axis, so the
legend is unnecessary. Notice that with the legend removed, the area used for graphing the data
is larger. If you want to achieve the same proportions in the graphing area, you will need to ad-
just the overall dimensions of the graph.
When a variable is mapped to fill , the default scale used is scale_fill_discrete() (equi-
valent to scale_fill_hue() ), which maps the factor levels to colors that are equally spaced
around the color wheel. There are other scales for fill , such as scale_fill_manual() . If you
use scales for other aesthetics, such as colour (for lines and points) or shape (for points), you
must use the appropriate scale. Commonly used scales include:
scale_fill_discrete()
scale_fill_hue()
scale_fill_manual()
scale_fill_grey()
scale_fill_brewer()
scale_colour_discrete()
scale_colour_hue()
scale_colour_manual()
scale_colour_grey()
scale_colour_brewer()
scale_shape_manual()
scale_linetype()
Changing the Position of a Legend
Problem
You want to move the legend from its default place on the right side.
Solution
Use theme(legend.position=...) . It can be put on the top, left, right, or bottom by using
one of those strings as the position ( Figure 10-2 , left):
p <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) + geom_boxplot() +
scale_fill_brewer(palette = "Pastel2" )
p + theme(legend.position = "top" )
Search WWH ::




Custom Search