How to Reorder Factor Levels in R

Spread the love

Reordering factor levels in R is an essential skill in data analysis, primarily because the order of factor levels influences the results of statistical models and the arrangement of bars in bar plots. A factor is a categorical variable that stores both the actual values and their order. This article will cover the various ways to reorder factor levels in R, explore the significance of doing so, and provide examples to illustrate each method.

Understanding Factors in R

In R, a factor is a data object used to categorize and store discrete values. Factors are integral in statistical modeling and are pivotal when dealing with categorical data. A factor has levels that represent the distinct categories in the dataset. The order of these levels is also crucial as it affects the reference level in statistical models and the presentation of data in visual plots.

# Creating a factor
fruits <- factor(c("Apple", "Banana", "Cherry"))

Significance of Reordering Factor Levels

The default order of factor levels in R is alphabetical. However, this may not always be suitable, especially when levels have an inherent order, like “Low”, “Medium”, and “High”, or when one needs a custom order for analytical or presentation purposes.

Reordering factor levels can impact:

  1. Statistical Analysis: The reference level affects the interpretation of the coefficients in regression models.
  2. Data Visualization: The order of levels determines the arrangement in plots, affecting the visual interpretation of the data.

Basic Reordering Using the factor() Function

The simplest way to reorder factor levels is by using the factor() function itself, specifying the desired level order in the levels argument:

# Reordering factor levels
fruits_reordered <- factor(fruits, levels = c("Cherry", "Banana", "Apple"))

Reordering Factor Levels with forcats

The forcats package, part of the tidyverse, provides several functions to work with factor levels. The fct_relevel() function is particularly useful for reordering levels:

fruits_reordered <- fct_relevel(fruits, "Cherry", "Banana", "Apple")

Reordering by Frequency

In some scenarios, it is useful to reorder levels by their frequency:


fruits <- factor(c("Apple", "Banana", "Cherry", "Apple", "Banana"))

# Reordering by frequency
fruits_reordered <- fruits %>% fct_infreq()

Here, fct_infreq() reorders the levels by their frequencies, placing the most frequent level first.

Reordering Levels by Another Variable

Often, it is necessary to reorder factor levels based on another variable. For instance, reordering product categories by average sales:


# Example data frame
df <- data.frame(
  Category = factor(c("Fruits", "Vegetables", "Dairy")),
  Sales = c(2300, 1500, 3100)

# Reordering factor levels by Sales
df <- df %>%
  arrange(Sales) %>%
  mutate(Category = factor(Category, levels = unique(Category)))

Advanced Reordering with Custom Functions

For more advanced and specific reordering needs, creating custom functions can be an effective approach. A custom function can consider multiple conditions and rules to create a tailored order of factor levels. For instance, one might write a function to categorize weekdays, considering both the day of the week and whether it is a working day or a weekend.


Reordering factor levels is a foundational skill in R, deeply intertwined with categorical data analysis and visualization. The ability to manipulate the order of factor levels allows for more accurate and insightful statistical models, more coherent and effective visual representations, and overall, a more refined and precise approach to exploring and understanding data.

Whether using basic functions like factor(), leveraging specialized functions from packages like forcats, or creating custom ordering functions, the methodology selected should align with the analytical goals and the nature of the data. By mastering the various techniques of reordering factor levels, one can significantly enhance the depth and clarity of their data analysis endeavors in R.

Posted in RTagged

Leave a Reply