# packages library(ggplot2)library(dplyr)
# set global default themetheme_set(theme_minimal(base_size = 14) + theme(axis.text.x = element_text(angle = 30, hjust = 1)))
# create a bar plot<- mpg %>% p ggplot(aes(x = class, fill = class)) + geom_bar(alpha = .4, color = "black") + scale_fill_brewer(palette = "Set1") p
Reorder Legend Keys and Labels in ggplot2
This tutorial explains how to reorder the legendary keys and labels of categorical (discrete) variables in a legend.
If you wish to reorder the legend colorbar of a continuous (numeric) variable, check here instead.
Start with a barplot
In the following bar plot, the legend keys by default are arranged in the same order as the bars, based on the numeric-alphabetical order of the class
levels.
If you wish to reorder both the bars and the legend keys at the same time, check this guide. The following tutorial instead illustrates how to reorder the legend keys independent of the bar order.
Reverse the legend key order
Use guides()
to address non-data related outlook of scales (legends and axes). The reverse
argument reverse the order of legend keys while retaining the same color scale (the color assignment to the car classes remains the same). As the legend is associated with the fill
aesthetic, use fill
in the guides syntax.
+ guides(fill = guide_legend(reverse = T)) p
Equivalently, the guide
syntax can be put inside the scale_*()
function. (This updated scale_fill_brewer()
overwrites the earlier one, and brings a message reminder in the console).
+ scale_fill_brewer( p palette = "Set1", guide = guide_legend(reverse = T))
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
Specify any particular order “manually”
As the fill
scale is discrete, the legend key order can be manually manipulated using the limits
argument in the scale_*()
function.
# order of 'class' to be displayed in the legend<- c( desired_order "subcompact", "compact", "minivan", "midsize", "2seater", "pickup", "suv")
+ scale_fill_brewer(palette = "Set1", limits = desired_order) p
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
This same technique can be also used to reorder categorical axial levels. In the following plot, the bars are arranged by the specified order, while the legend keys are arranged separately in the default numeric-alphabetical order.
+ scale_x_discrete(limits = desired_order) p
Extended Reading — 🚀 one level up!
There are several important techniques related with aforementioned scale_fill_brewer()
:
The fill scale in this tutorial employs the brewer palettes. As the variable
class
mapped tofill
is categorical, thebrewer
suffix is used (in place of the asterisk inscale_fill_*
); if a continuous variable is mapped tofill
, use suffixdistiller
instead.When using the viridis palettes, the syntax to reorder the legend keys remains similar:
+ scale_fill_viridis_d(option = "A", limits = desired_order) p
- If you wish to use the default fill scale, you may call
scale_fill_discrete()
:
+ scale_fill_discrete(limits = desired_order) p