Skip to content
Home ยป Reorder a ggplot2 bar chart by count

Reorder a ggplot2 bar chart by count

Plotting bars in ggplot2 is easy. Yet, in many cases, you want to order these bars according to their frequency (count) or according to any other numeric value. In this blog post, I show you three ways to achieve this.

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


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

The first solution is the dplyr way. Group the data frame and summarise the count and pass it to the ggplot function. In your aesthetics, you can use the reorder function to order the bars on their frequency.

df %>% 
	group_by(Pclass) %>% 
	summarise(count = n()) %>% 
	ggplot(aes(x = reorder(Pclass,(-count)), y = count)) + 
		geom_bar(stat = 'identity') + 

The second solution is the data.table way. It’s the same line of thought but the syntax is more concise.

ggplot(df[,.(count = .N), by = Pclass],aes(x = reorder(Pclass,(-count)), y = count)) + 
	geom_bar(stat = 'identity') + 

The third solution uses forcats. It’s another Tidyverse package to work with categorical variables. It offers a really handy function: fct_infreq() reorders according to a factor value’s frequency.

ggplot(df,aes(x = fct_infreq(Pclass))) + 
	geom_bar(stat = 'count') + 

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.

2 thoughts on “Reorder a ggplot2 bar chart by count”

Leave a Reply

Your email address will not be published. Required fields are marked *