Graphics Reference
In-Depth Information
See Also
To reverse the order of the legend, see
Reversing the Order of Items in a Legend
.
To change the order of factor levels, see
Changing the Order of Factor Levels
. To order legend
items based on values in another variable, see
Changing the Order of Factor Levels Based on
Reversing the Order of Items in a Legend
Problem
You want to reverse the order of items in a legend.
Solution
Add
guides(fill=guide_legend(reverse=TRUE))
to reverse the order of the legend, as in
or
size
):
# The base plot
p
<-
ggplot(PlantGrowth, aes(x
=
group, y
=
weight, fill
=
group))
+
geom_boxplot()
p
# Reverse the legend order
p
+
guides(fill
=
guide_legend(reverse
=
TRUE
TRUE
))