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
Data Values .
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
Figure 10-7 (for other aesthetics, replace fill with the name of the aesthetic, such as colour
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 ))
Search WWH ::




Custom Search