Order Bars in ggplot2 bar graph
Last Updated :
28 Apr, 2025
When creating bar graphs in ggplot2, it's often crucial to control the order of bars on the x-axis based on a specific variable. This guide explores the theory behind ordering bars in ggplot2 bar graphs and provides various examples to illustrate different scenarios in the R Programming Language.
In ggplot2, the reorder() function is commonly used to reorder bars on the x-axis based on a specified variable. The reorder() function takes two arguments: the variable you want to reorder and the variable you want to use for ordering. It returns a factor with the levels reordered based on the values of the second argument.
R
# Sample data
data <- data.frame(
category = c("A", "B", "C", "D", "E"),
value = c(15, 8, 5, 12, 20)
)
Basic Bar Plot
In this example, we'll create a basic bar plot without any specific ordering:
R
# Basic bar plot
basic_plot <- ggplot(data, aes(x = category, y = value)) +
geom_bar(stat = "identity", fill = "skyblue") +
labs(title = "Basic Bar Plot")
# Display the basic plot
print(basic_plot)
Output:

In this basic bar plot, the categories are plotted in the order they appear in the dataset.
Ordered Bar Plot
Now, let's reorder the bars based on the value variable:
R
# Ordered bar plot
ordered_plot <- ggplot(data, aes(x = reorder(category, -value), y = value)) +
geom_bar(stat = "identity", fill = "skyblue") +
labs(title = "Ordered Bar Plot")
# Display the ordered plot
print(ordered_plot)
Output:

Here, the reorder(category, -value) function is used to reorder the bars on the x-axis based on the value variable in descending order. The resulting plot will have bars ordered from highest to lowest values.
Custom Ordering
You can also specify a custom order for the bars. In this example, let's order the bars alphabetically:
R
# Custom ordered bar plot in ascending order based on values
custom_order_plot_ascending_values <- ggplot(data, aes(x = reorder(category, value,
FUN = function(x) sum(x)),
y = value)) +
geom_bar(stat = "identity", fill = "skyblue") +
labs(title = "Custom Ordered Bar Plot (Ascending by Values)")
# Display the custom ordered plot in ascending order based on values
print(custom_order_plot_ascending_values)
Output:

The reorder() function is adjusted to include the value variable, and a custom function is provided to reorder the categories in ascending order based on their values. The FUN argument now calculates the sum of the values for each category, ensuring ascending order by the total value.
Conclusion
Ordering bars in ggplot2 bar graphs is a valuable technique for enhancing the visual representation of your data. Whether you're emphasizing certain categories or presenting data in a specific order, the reorder() function provides flexibility in achieving your desired bar order. Adapt these examples to suit your specific dataset and visualization goals.
Similar Reads
Stacked Bar Chart in R ggplot2 A stacked bar plot displays data using rectangular bars grouped by categories. Each group represents a category, and inside each group, different subcategories are stacked on top of each other. It also shows relationships between groups and subcategories, making them useful for various data analysis
4 min read
Adding error bars to a line graph with ggplot2 in R ggplot2 is an R language plotting package that creates complex plots from data in a data frame. It describes what variables to plot, how they are displayed, and general visual properties. It can add error bars, crossbars, line range, point range in our graph. This article is solely dedicated to addi
3 min read
Reorder Facets in ggplot2 Plot in R In this article, we will be looking at an approach to reorder the facets in the ggplot2 plot in R programming language. To reorder the facets accordingly of the given ggplot2 plot, the user needs to reorder the levels of our grouping variable accordingly with the help of the levels function and requ
2 min read
How to personalize easily ggplot2 graphs in R In this article, we are going to learn how to personalize easily ggplot2 graphs in the R programming language. Data visualization is an essential tool for understanding and communicating complex data sets. One of the most popular and powerful visualization libraries in R is ggplot2, which offers a w
11 min read
How To Reorder Boxplots in R with ggplot2? In this article, we will discuss how to reorder the boxplot with ggplot2 in R Programming Language. To reorder the boxplot we will use reorder() function of ggplot2. Syntax: ggplot(sample_data, aes(x=reorder(name,value),y=value)) By default, ggplot2 orders the groups in alphabetical order. But for b
2 min read
Combine bar and line chart in ggplot2 in R Sometimes while dealing with hierarchical data we need to combine two or more various chart types into a single chart for better visualization and analysis. These are known as âCombination chartsâ. In this article, we are going to see how to combine a bar chart and a line chart in R Programming Lang
4 min read