Graphics Reference
In-Depth Information
See Also
See
Changing the Appearance of Text
for more on controlling the appearance of text.
Using Labels with Multiple Lines of Text
Problem
You want to use legend labels that have more than one line of text.
Solution
Set the
labels
in the scale, using
\n
to represent a newline. In this example, we'll use
p
<-
ggplot(PlantGrowth, aes(x
=
group, y
=
weight, fill
=
group))
+
geom_boxplot()
# Labels that have more than one line
p
+
scale_fill_discrete(labels
=
c(
"Control"
,
"Type 1\ntreatment"
,
"Type 2\ntreatment"
))
Figure 10-17. Left: multiline legend labels; right: with increased key height and reduced line spa-
cing
Discussion
As you can see in the version on the left in
Figure 10-17
, with the default settings the lines of
text will run into each other when you use labels that have more than one line. To deal with this
problem, you can increase the height of the legend keys and decrease the spacing between lines,