How to Sort Bars by Value in ggplot2 (With Example)


Often you may want to create a bar plot using ggplot2 and sort the bars in the plot based on value.

There are two common ways to do so:

Method 1: Sort Bars in ggplot2 in Ascending Order

library(ggplot2)

ggplot(df, aes(x=reorder(team, points), y=points)) +
  geom_bar(stat='identity')

This particular example creates a bar plot and sorts the bars in ascending order based on the value in the points column of the data frame.

Method 2: Sort Bars in ggplot2 in Descending Order

library(ggplot2)

ggplot(df, aes(x=reorder(team, -points), y=points)) +
  geom_bar(stat='identity')

This particular example creates a bar plot and sorts the bars in descending order based on the value in the points column of the data frame.

In both of these examples we use the reorder() function to reorder the values of the team column based on the values in the points column.

By default, the reorder() function orders the bars in ascending order.

By placing a negative sign in front of the points string, we are able to sort by descending order.

Note: By using stat=’identity’ in geom_bar() we are able to tell ggplot2 to calculate the sum of values in the points column as opposed to simply counting the number of occurrences of values in the points column.

The following examples show how to use each of these methods in practice with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'),
                 points=c(22, 25, 18, 13, 41, 23, 30, 40, 22, 15))

#view data frame
df

   team points
1     A     22
2     B     25
3     C     18
4     D     13
5     E     41
6     F     23
7     G     30
8     H     40
9     I     22
10    J     15

Example 1: Sort Bars in ggplot2 in Ascending Order

We can use the following syntax to create a bar plot in ggplot2 with the team values along the x-axis, the points values along the y-axis and the bars sorted based on ascending order of the points values:

library(ggplot2)

#create bar plot with bars sorted in ascending order
ggplot(df, aes(x=reorder(team, points), y=points)) +
  geom_bar(stat='identity')

This produces the following bar chart:

geom_bar sort bars in ascending order by value

The x-axis displays the individual team names and the y-axis displays the sum of points scored by each team.

Notice that the bars in the plot are sorted in ascending order based on the values in the points column.

Example 2: Sort Bars in ggplot2 in Descending Order

We can use the following syntax to create a bar plot in ggplot2 with the team values along the x-axis, the points values along the y-axis and the bars sorted based on descending order of the points values:

library(ggplot2)

#create bar plot with bars sorted in descending order
ggplot(df, aes(x=reorder(team, -points), y=points)) +
  geom_bar(stat='identity')

This produces the following bar chart:

geom_bar in order based on descending value

Once again, the x-axis displays the team names and the y-axis displays the sum of points scored by each team.

Notice that the bars in the plot are sorted in descending order based on the values in the points column.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Adjust Space Between Bars in ggplot2
How to Remove NAs from Plot in ggplot2
How to Change Colors of Bars in Stacked Bart Chart in ggplot2

Featured Posts

Leave a Reply

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