• 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 Reverse Order in Facet in ggplot

datavizpyr · May 15, 2022 ·

In this tutorial, we will learn how to reverse the order of facets, multiple small plots, made with ggplot2 in R. We will use R package forcats’s handy function fct_rev() to reverse the order of facets made using facet_wrap() function in ggplot2.
Let us load tidyverse suite of R packages and palmer penguin dataset for making a plot with facets.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

Simple Scatter Plot with Facets

Here we make a scatter plot facetted by third variable, by adding facet_wrap() function with the third variable

penguins %>%
  drop_na()%>%
  ggplot(aes(flipper_length_mm, body_mass_g, color = sex)) + 
  geom_point() +
  facet_wrap(~species)+
  theme(legend.position = "bottom")
ggsave("facet_wrap_example_ggplot2.png", width=9,height=6)
facetting with ggplot2
facetting with ggplot2

Reversing the order of Facets in ggplot2

In the previous plot we can see that, facets are ordered in alphabetical order by default. One of the ways to reverse the order of facets using forcats’ fct_rev() function while specifying the variable that we want to facet by. In this example, we used fct_rev(species) inside facet_wrap(), like facet_wrap(~fct_rev(species)).

penguins %>%
  drop_na() %>%
  ggplot(aes(flipper_length_mm, body_mass_g, color = sex)) + 
  geom_point() +
  facet_wrap(~fct_rev(species))+
  theme(legend.position = "bottom")
ggsave("facet_wrap_reverse_order_example_ggplot2.png", width=9, height=6)
How to reverse the order of facets in R
How to reverse the order of facets in R

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: ggplot2, R Tagged With: reverse facet order ggplot2

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