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.

# 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 plotp <- mpg %>% ggplot(aes(x = class, fill = class)) + geom_bar(alpha = .4, color = "black") + scale_fill_brewer(palette = "Set1")p

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.

p + guides(fill = guide_legend(reverse = T))

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).

p + scale_fill_brewer(  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 legenddesired_order <- c(  "subcompact", "compact", "minivan", "midsize",  "2seater",  "pickup", "suv")
p + scale_fill_brewer(palette = "Set1", limits = desired_order)
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.

p + scale_x_discrete(limits = desired_order)


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 to fill is categorical, the brewer suffix is used (in place of the asterisk in scale_fill_*); if a continuous variable is mapped to fill, use suffix distiller instead.

  • When using the viridis palettes, the syntax to reorder the legend keys remains similar:

p + scale_fill_viridis_d(option = "A", limits = desired_order)
  • If you wish to use the default fill scale, you may call scale_fill_discrete():
p + scale_fill_discrete(limits = desired_order)