5.5 Overall look -
And finally, everything else on a plot - from font to background to the space between facets, can be changed using the
As you saw in the previous chapter, in addition to its default grey background,
ggplot2 also comes with a few built-in themes, namely,
These produce good looking plots that may already be publication ready.
But if we do decide to tweak them, then the main
theme() arguments we use are
Note that all of these go inside the
theme(), and that the
axis.title arguments are usually followed by
= element_text() as shown in the examples below.
5.5.1 Text size
The way the
axis.title arguments of
theme() work is that if you specify
.y it gets applied on that axis alone.
But not specifying these, applies the change on both.
vjust (vertical justification) options can be useful if your axis text doesn’t fit well and overlaps.
It doesn’t usually make sense to change the colour of the font to anything other than
"black", we are using green and red here to indicate which parts of the plot get changed with each line (Figure 5.10).
5.5.2 Legend position
The position of the legend can be changed using the
legend.position argument within
theme(). It can be positioned using the following words:
"right", "left", "top", "bottom".
Or to remove the legend completely, use
Alternatively, we can use relative coordinates (0–1) to give the legend a relative x-y location (Figure 5.11):
theme(legend.) options can be used to change the size, background, spacing, etc., of the legend.
However, for modifying the content of the legend, you’ll have to use the
ggplot()’s defaults are very good, and we rarely need to go into this much tweaking using both the
guides() functions. But it is good to know what is possible.
For example, this is how to change the number of columns within the legend (Figure 5.12):