Graphics Reference
In-Depth Information
Figure 10-14. Modified legend label order and manually specified labels (note that the x-axis la-
bels and their order are unchanged)
If you have one variable mapped to two separate aesthetics, the default is to have a single legend
that combines both. If you want to change the legend labels, you must change them for both
scales; otherwise you will end up with two separate legends, as shown in
Figure 10-15
:
# The base plot
p
<-
ggplot(heightweight, aes(x
=
ageYear, y
=
heightIn, shape
=
sex, colour
=
sex))
+
geom_point()
p
# Change the labels for one scale
p
+
scale_shape_discrete(labels
=
c(
"Female"
,
"Male"
))
# Change the labels for both scales
p
+
scale_shape_discrete(labels
=
c(
"Female"
,
"Male"
))
+
scale_colour_discrete(labels
=
c(
"Female"
,
"Male"
))