• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How to specify the order of panes in facet_wrap

datavizpyr · July 11, 2024 ·

Last updated on July 14, 2024

In this tutorial, we will learn how to change the order of panes in a plot made with facet_wrap() or facet_grid(). We will use forcats package’s function fct_relevel() to specify the order we want while plotting.

library(palmerpenguins)
library(tidyverse)
theme_set(theme_bw(16))
penguins |> head()

# A tibble: 6 × 8
  species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
  <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
1 Adelie  Torgersen           39.1          18.7               181        3750
2 Adelie  Torgersen           39.5          17.4               186        3800
3 Adelie  Torgersen           40.3          18                 195        3250
4 Adelie  Torgersen           NA            NA                  NA          NA
5 Adelie  Torgersen           36.7          19.3               193        3450
6 Adelie  Torgersen           39.3          20.6               190        3650
# ℹ 2 more variables: sex <fct>, year <int>

Let us make a scatterplot with facets using facet_wrap() function in ggplot2.

penguins |>
  drop_na() |>
  ggplot(aes(x=bill_length_mm, body_mass_g, color=sex))+
  geom_point()+
  geom_smooth(method="lm")+
  theme(legend.position = "bottom")+
  facet_wrap(~species, scales = "free_x")
ggsave("how_to_specify_order_of_plots_in_facet_wrap.png")

How to specify order of plots in a plot with facet_wrap()
How to specify order of plots in a plot with facet_wrap()

Change order of plots in facet_wrap() with fct_relevel()

We specify the order of panes in the plot by using fct_relevel() inside facet_wrap() function.

penguins |>
  drop_na() |>
  ggplot(aes(x=bill_length_mm, body_mass_g, color=sex))+
  geom_point()+
  geom_smooth(method="lm")+
  #facet_wrap(~species)+
  theme(legend.position = "bottom")+
  facet_wrap(~forcats::fct_relevel(species, "Gentoo", "Adelie", "Chinstrap"),
             scales = "free_x")
ggsave("specify_order_of_plots_with_fct_relevel_facet_wrap.png")
Specify order of plots with fct_ relevel() in facet_wrap()
Specify order of plots with fct_ relevel() in facet_wrap()

If you are interested in simply reversing the order of plots in facet_wrap(), we can use forcats’ fct_rev() function as shown in this post

    How to Reverse Order in Facet in ggplot

Explore the Complete ggplot2 Guide

35+ tutorials with code: scatterplots, boxplots, themes, annotations, facets, and more—tested and beginner-friendly.

Visit the ggplot2 Hub → No fluff—just code and visuals.

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: facet_wrap(), ggplot2, R Tagged With: change plot order in facet_wrap()

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version