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