# Sorting the bars of a bar chart in increasing or decreasing order

Here is a simple trick to make the bars in a bar chart appear in order of their height - highest to lowest (or lowest to highest).

Let’s say we want to plot the number of cars of each class that we have in the mpg dataset.

library(dplyr)
library(ggplot2)
mpg %>%
ggplot() +
geom_bar(aes(x = class))

If you look at class(mpg$class) you will see that it is: ## [1] "character" However, ggplot converts it into factor and if you convert it into factor you will notice that the orders in which the bars appear in the bar chart above is the same order in which the list has recorded the levels of the variable class. Let’s take a look at it below: levels( as.factor(mpg$class)
)
## [1] "2seater"    "compact"    "midsize"    "minivan"    "pickup"
## [6] "subcompact" "suv"

Let’s say we want to change that order and we would like it to appear in decreasing (or increasing) order of count of each level of the dataset.

## Enter reorder()

reorder, as the name suggests, reorders the levels of a factor variable. The reordering happens based on another numeric variable.

In this case if we want to reorder the levels of class in decreasing (or increasing) order of counts we can do this:

class_reordered <- reorder(mpg$class, mpg$class, function(x) -length(x))
levels(class_reordered)
## [1] "suv"        "compact"    "midsize"    "subcompact" "pickup"
## [6] "minivan"    "2seater"

We can see that levels have been reordered based on counts of each type of class. Let’s take a look at how it will affect our bar plot.

mpg %>%
ggplot() +
geom_bar(aes(x = reorder(class, class, function(x) -length(x)))) +
xlab("class")

There we go, job done. If we would like to reverse that order we can do so by changing the function argument in reorder.

mpg %>%
ggplot() +
geom_bar(aes(x = reorder(class, class, function(x) length(x)))) +
xlab("class")

There you go.