Home » Reorder a ggplot2 bar chart by count

Reorder a ggplot2 bar chart by count

  • by
  • 1 min read

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

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.

Leave a Reply

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