Home » Custom order for ggplot2 bar charts

# Custom order for ggplot2 bar charts

• 1 min read

In my previous post, I explained how you can reorder bar charts based on the frequency of each value. In this blog post, I explain how you can set a custom order.

First, let’s load the libraries and create the titanic data set.

rm(list=ls())
library(titanic)
library(data.table)
library(dplyr)
library(forcats)
library(ggplot2)
library(ggthemes)

df <- data.table(titanic::titanic_train)
df[,Pclass := as.factor(Pclass)]

Assume we want to order our bar chart by “Pclass”. However, 3rd class is the lowest, while 1st class is the highest. Let’s see how we can achieve this.

First up, the dplyr way. By using the mutate function, I create a new column with a reordered version of the “Pclass” column. Passing this to ggplot2 does exactly whay we want.

df %>%
mutate(Pclass = factor(Pclass,levels = c('3','2','1'))) %>%
ggplot(aes(x = Pclass)) +
geom_bar() +
theme_clean()

Next, the data.table way. The idea is once again as in line with the dplyr solution. However, more concise, less lines.

ggplot(df[,Pclass := factor(Pclass,levels = c('3','2','1'))],aes(x = Pclass)) +
geom_bar() +
theme_clean()

Unsurprisingly, ggplot2 offers an out-of-the-box solution to set a custom order using the scale_x_discrete function.

ggplot(df,aes(x = Pclass)) +
geom_bar() +
theme_clean() +
scale_x_discrete(limits = c('3','2','1'))

By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!

Great success!

### Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.