Reorder Facets in ggplot2 Plot in R (Example)

 

This tutorial illustrates how to fix the ordering of facets in a ggplot2 graph in R.

The content of the tutorial is structured as follows:

Here’s the step-by-step process…

 

Example Data, Packages & Basic Graph

Let’s first create some example data in R:

data <- data.frame(x1 = 1:4,                  # Create example data
                   x2 = 4:1,
                   group = LETTERS[1:4])
data                                          # Show data in console
#   x1 x2 group
# 1  1  4     A
# 2  2  3     B
# 3  3  2     C
# 4  4  1     D

As you can see based on the previous output of the RStudio console, our example data has two numeric columns and a grouping variable.

If we want to plot our data with the ggplot2 package, we also have to install and load ggplot2:

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

Next, we can draw a graphic of our data:

ggp <- ggplot(data, aes(x1, x2)) +            # Create facet plot with default order
  geom_point() +
  facet_grid(.~group)
ggp                                           # Draw facet plot

 

r graph figure 1 reorder facets ggplot2

 

Figure 1 shows the output of the previously shown syntax – A facet plot with facets in alphabetical order.

 

Example: Reordering Facets of Facet Plot Using relevel Function

In this Example, I’ll illustrate how to adjust the sorting of a ggplot2 facet plot in R. As a first step, we have to reorder the levels of our grouping variable using the relevel function:

data_new <- data                              # Replicate data
data_new$group <- factor(data_new$group,      # Reordering group factor levels
                         levels = c("B", "A", "C", "D"))

Now, we can draw our plot again:

ggp_new <- ggplot(data_new, aes(x1, x2)) +    # Create facet plot again
  geom_point() +
  facet_grid(.~group)
ggp_new                                       # Print new facet plot

 

r graph figure 2 reorder facets ggplot2

 

Figure 2 shows the output of the previous R programming syntax – A reordered facet plot created with the ggplot2 add-on package.

 

Video & Further Resources

Do you need further information on the examples of this article? Then you could watch the following video that I have published on my YouTube channel. In the video, I’m explaining the contents of this article.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, I can recommend reading the other articles of this website:

 

Summary: In this tutorial, I illustrated how to reorder facets in a ggplot2 facet plot in the R programming language.

In this tutorial, we have used the facet_plot function. However, please note that it would be possible to use a similar R syntax when using the related facet_wrap function.

If you have further questions, let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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