forcats::fct_rev()

Function of the Week

fct_rev(): Reverse factor levels
Author

Michael Daily

Published

March 5, 2025

1 Factor Reverse: fct_rev()

This document discusses fct_rev() and its use. fct_rev() is from the forcats package and, similar to other functions from forcats, is a tool for handling factor type variables.

#load packages
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(datasets)
library(forcats)
library(janitor)

Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test
#ChickWeight dataset
chick_wgt <-ChickWeight %>% clean_names()
glimpse(chick_wgt)
Rows: 578
Columns: 4
$ weight <dbl> 42, 51, 59, 64, 76, 93, 106, 125, 149, 171, 199, 205, 40, 49, 5…
$ time   <dbl> 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 1…
$ chick  <ord> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
$ diet   <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

1.1 What does it do and what is it for?

1.1.1 What fct_rev() does:

fct_rev() does what it sounds like. It reverses the levels of the factor variable. It takes only one argument which is a factor (or character vector).

#look at the levels of diet
levels(chick_wgt$diet)
[1] "1" "2" "3" "4"
#revers the levels
levels(fct_rev(chick_wgt$diet))
[1] "4" "3" "2" "1"

1.1.2 What fct_rev() is useful for:

fct_rev() is useful for plotting factors on the y-axis. By default, the levels are plotted with the first level at the bottom and the last at the top as shown below.

ggplot(chick_wgt) + 
  aes(y=diet,
      x=weight,
      fill=diet) +
  geom_boxplot() +
  scale_fill_viridis_d()+
  theme_bw()+
  theme(axis.text.y=element_blank(),
        plot.title=element_text(hjust=.5))+
  labs(y='Diet', x='Weight', fill='Diet', title='Weight Distribution per Diet (original order)')

fct_rev() makes it easy to plot the reverse with the first level at the top and the last level at the bottom as shown in the following figure.

ggplot(chick_wgt) + 
  aes(y=fct_rev(diet),
      x=weight,
      fill=diet) +
  geom_boxplot() +
  scale_fill_viridis_d()+
  theme_bw()+
  theme(axis.text.y=element_blank(),
        plot.title=element_text(hjust=.5))+
  labs(y='Diet', x='Weight', fill='Diet', title='Weight Distribution per Diet (reversed order)')

1.2 Is it helpful?

In cases where you want to plot a factor on the y-axis, this is a helpful function; this is particularly true when the factor variable has many levels and it would be tedious to reorder by hand. Aside from reordering levels for a figure, this function seems like it would rarely, if ever, be useful.