Graphics Reference
In-Depth Information
Figure 10-10. Left: default legend with a variable mapped to shape and colour; middle: with shape
renamed; right: with both shape and colour renamed
To change the title (
Figure 10-10
, right), you need to set the name for both of them. If you
change the name for just one, it will result in two separate legends (
Figure 10-10
, middle):
# Change just shape
hw1
+
labs(shape
=
"Male/Female"
)
# Change both shape and colour
hw1
+
labs(shape
=
"Male/Female"
, colour
=
"Male/Female"
)
It is also possible to control the legend title with the
guides()
function. It's a little more verbose,
but it can be useful when you're already using it to control other properties:
p
+
guides(fill
=
guide_legend(title
=
"Condition"
))
Changing the Appearance of a Legend Title
Problem
You want to change the appearance of a legend title's text.
Solution
p
<-
ggplot(PlantGrowth, aes(x
=
group, y
=
weight, fill
=
group))
+
geom_boxplot()
p
+
theme(legend.title
=
element_text(face
=
"italic"
, family
=
"Times"
, colour
=
"red"
,
size
=
14
))