Order Bars of ggplot2 Barchart in R (4 Examples)

 

In this R tutorial you’ll learn how to order the bars of a ggplot2 barchart.

The content is structured as follows:

Here’s how to do it!

 

Creation of Example Data

First, we need to create some example data to illustrate the following examples:

data <- data.frame(x = c("A", "B", "C", "D", "E"),            # Create example data
                   y = c(0.5, 2, 1.2, -0.2, 0.7))

Furthermore, we need to install and load the ggplot2 package to R:

install.packages("ggplot2")                                   # Install ggplot2 package
library("ggplot2")                                            # Load ggplot2 package

Now, we can draw a basic barplot with the following R code:

ggplot(data, aes(x, y)) +                                     # Create basic barchart
  geom_bar(stat = "identity")

 

ggplot2 barchart in r

Figure 1: Basic Barchart in ggplot2 R Package.

 

Figure 1 shows the output of the previous R code – An unordered ggplot2 Barplot in R.

 

Example 1: Ordering Bars Manually

If we want to change the order of the bars manually, we need to modify the factor levels of our ordering column. We can do that with the following R syntax:

data1 <- data                                                 # Replicate original data
data1$x <- factor(data1$x,                                    # Change ordering manually
                  levels = c("B", "D", "E", "C", "A"))

If we now use exactly the same ggplot2 syntax as before, we get the following plot:

ggplot(data1, aes(x, y)) +                                    # Manually ordered barchart
  geom_bar(stat = "identity")

 

manually ordered ggplot2 barchart in r

Figure 2: Manual Order of Bars.

 

Figure 2 illustrates the new ordering of our barchart.

 

Example 2: Barchart with Increasing Order

Let’s assume that we want to sort our barplot by the size of the bars. Then we can easily use the sort function to order the factor levels according to the values of our bars:

data2 <- data                                                 # Replicate original data
data2$x <- factor(data2$x,                                    # Factor levels in increasing order

levels = data2$x[order(data2$y)])

ggplot(data2, aes(x, y)) +                                    # Increasingly ordered barchart
  geom_bar(stat = "identity")

 

ggplot2 barchart in r with increasing order

Figure 3: Increasing Order of Bars.

 

As you can see in Figure 3, our bars were sorted in decreasing order.

 

Example 3: Barchart with Decreasing Order

Of cause, you could also do the sorting the other way around. The following R code sorts our bars in decreasing order:

data3 <- data                                                 # Replicate original data
data3$x <- factor(data3$x,                                    # Factor levels in decreasing order
                  levels = data3$x[order(data3$y, decreasing = TRUE)])
ggplot(data3, aes(x, y)) +                                    # Decreasingly ordered barchart
  geom_bar(stat = "identity")

 

ggplot2 barchart in r with decreasing order

Figure 4: Decreasing Order of Bars.

 

That’s it!

 

Video, Further Resources & Summary

Would you like to learn more about the ordering of bargraphs using ggplot2? Then you might have a look at the following video of my YouTube channel. In the video, I illustrate the content of this post.

 

 

Furthermore, I can recommend to have a look at some of the related articles of this website. A selection of articles about the ordering of factors is listed here.

 

You learned in this article how to reorder factors to plot the bars of a ggplot in a specified order in R programming. Please let me know in the comments, if you have any additional questions. Furthermore, don’t forget to subscribe to my email newsletter for updates on the newest tutorials.

 



Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top