• 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
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to specify the order of panes in facet_wrap

datavizpyr · July 11, 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

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

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

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

Go to mobile version