# How to arrange ggplot (barplot) bars in ascending or descending order

## using reorder()

One of the reasons you’d see a bar plot made with ggplot2 with no ascending / descending order - ordering / arranged is because, By default, ggplot arranges bars in a bar plot alphabetically.

But most of the times, it would make more sense to arrange it based on the y-axis it represents (rather than alphabetically).

It could be your month-wise time series or high-medium-low bars - these are some examples where an alphabetically-sorted bar chart wouldn’t make sense in fact would hinder the process of data communication.

### How to arrange a ggplot2 bar plot in an order

This code tries to plot the average mpg of car brands in a properly sorted/arranged ggplot bar plot.

library(dplyr)
library(ggplot2)
mtcars %>% tibble::rownames_to_column('Car') %>%
tidyr::separate('Car',c('Brand','Model'), remove = F) %>%
group_by(Brand) %>%
summarize(avg_mpg = mean(mpg)) %>%
ggplot() + geom_bar(aes(reorder(Brand,avg_mpg),avg_mpg), stat = 'identity') +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) + labs(
title = 'How to arrange Ggplot Bar plot',
x = 'mpg') 

The function that we used here to arrange the bars is reorder(). Technically, it’s the data that we’re rearranging (not the bars) but that reordered factors help us in arranged ggplot bars.