How can the order of items in a ggplot2 legend be changed? 2

How can the order of items in a ggplot2 legend be changed?

The order of items in a ggplot2 legend can be changed by using the “limits” function in the “scale” argument. This allows for the reordering of items in the legend according to a specified pattern or desired sequence. Additionally, the “guides” function can also be utilized to customize the appearance and position of the legend within the ggplot2 graph. By using these functions, the order of items in the legend can be easily modified, providing greater flexibility and customization for data visualization.

Change Order of Items in ggplot2 Legend


You can use the following syntax to change the order of the items in a legend:

scale_fill_discrete(breaks=c('item4', 'item2', 'item1', 'item3', ...)

The following example shows how to use this syntax in practice.

Example: Change Order of Items in ggplot2 Legend

Suppose we create the following plot in ggplot2 that displays multiple boxplots in one plot:

library(ggplot2)

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C'),
                 points=c(6, 8, 13, 16, 10, 14, 19, 22, 14, 18, 24, 26))

#create multiple boxplots to visualize points scored by team
ggplot(data=df, aes(x=team, y=points, fill=team)) +
  geom_boxplot()

To change the order of the items in the legend, we can use the scale_fill_discrete() function as follows:

library(ggplot2)

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C'),
                 points=c(6, 8, 13, 16, 10, 14, 19, 22, 14, 18, 24, 26))

#create multiple boxplots to visualize points scored by team
ggplot(data=df, aes(x=team, y=points, fill=team)) +
  geom_boxplot() +
  scale_fill_discrete(breaks=c('B', 'C', 'A'))

ggplot2 boxplot with specific order of items in legend

Notice that the order of the items changed from: A, B, C to B, C, A.

We can also use the labels argument to change the specific labels used for the items in the legend:

library(ggplot2)

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C'),
                 points=c(6, 8, 13, 16, 10, 14, 19, 22, 14, 18, 24, 26))

#create multiple boxplots to visualize points scored by team
ggplot(data=df, aes(x=team, y=points, fill=team)) +
  geom_boxplot() +
  scale_fill_discrete(breaks=c('B', 'C', 'A'),
                      labels=c('B Team', 'C Team', 'A Team'))

Notice that the legend labels have changed.

Additional Resources

The following tutorials explain how to perform other common operations in ggplot2:

Cite this article

stats writer (2024). How can the order of items in a ggplot2 legend be changed?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-order-of-items-in-a-ggplot2-legend-be-changed/

stats writer. "How can the order of items in a ggplot2 legend be changed?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-the-order-of-items-in-a-ggplot2-legend-be-changed/.

stats writer. "How can the order of items in a ggplot2 legend be changed?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-order-of-items-in-a-ggplot2-legend-be-changed/.

stats writer (2024) 'How can the order of items in a ggplot2 legend be changed?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-order-of-items-in-a-ggplot2-legend-be-changed/.

[1] stats writer, "How can the order of items in a ggplot2 legend be changed?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can the order of items in a ggplot2 legend be changed?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top