Function reorder in R and ordering values
Categories:
Mastering Function Reordering and Value Ordering in R

Learn how to effectively reorder levels of a factor and sort values within data structures in R, enhancing data visualization and analysis.
In R, the order of factor levels and the sorting of values within vectors or data frames are crucial for both data manipulation and presentation. Incorrect ordering can lead to misleading visualizations and inefficient analysis. This article will guide you through various techniques to reorder function levels and sort values, providing practical examples and best practices.
Understanding Factors and Their Levels
Factors are a fundamental data type in R used to store categorical data. They have a predefined set of 'levels', which represent the unique categories. By default, R orders these levels alphabetically. However, for many analytical and visualization tasks, a custom order is often required. For instance, if you have a factor representing 'Low', 'Medium', 'High', alphabetical ordering would place 'High' before 'Low', which is semantically incorrect.
# Create a factor with default alphabetical ordering
data <- c("Medium", "High", "Low", "Medium", "Low")
factor_data <- factor(data)
print(factor_data)
print(levels(factor_data))
Default factor creation and level order
Reordering Factor Levels
There are several ways to reorder factor levels in R, depending on whether you want to specify the exact order, reorder based on another variable, or simply reverse the current order. The factor()
function itself is highly versatile for this purpose, especially when combined with the levels
argument.
flowchart TD A[Start with existing factor] --> B{Desired Order?} B -->|Specific Order| C[Use factor(..., levels = c(...))] B -->|Order by another variable| D[Use reorder() or forcats::fct_reorder()] B -->|Reverse Order| E[Use factor(..., levels = rev(levels(...)))] C --> F[Result: Factor with custom levels] D --> F E --> F
Decision flow for reordering factor levels
# 1. Reorder levels manually
ordered_factor <- factor(data, levels = c("Low", "Medium", "High"))
print(ordered_factor)
print(levels(ordered_factor))
# 2. Reorder levels based on a numeric variable (e.g., mean value)
# Requires a data frame for demonstration
df <- data.frame(
category = factor(c("A", "B", "C", "A", "B", "C")),
value = c(10, 50, 30, 12, 55, 32)
)
# Using `reorder` from base R
df$category_reordered <- with(df, reorder(category, value, FUN = mean))
print(levels(df$category_reordered))
# Using `fct_reorder` from `forcats` package (part of tidyverse)
# install.packages("forcats") # if not already installed
library(forcats)
df$category_fct_reordered <- fct_reorder(df$category, df$value, .fun = mean)
print(levels(df$category_fct_reordered))
Examples of reordering factor levels using different methods
ggplot2
, correctly ordered factor levels are essential for creating meaningful bar charts, box plots, and other categorical visualizations. Always ensure your factors are ordered as desired before plotting.Ordering Values within Vectors and Data Frames
Beyond factor levels, you often need to sort the actual values within a vector or reorder rows in a data frame based on one or more columns. R provides powerful functions like sort()
for vectors and order()
or arrange()
(from dplyr
) for data frames.
# Sorting a numeric vector
num_vec <- c(5, 2, 8, 1, 9)
sorted_num_vec <- sort(num_vec)
print(sorted_num_vec)
# Sorting a character vector
char_vec <- c("apple", "zebra", "banana", "cat")
sorted_char_vec <- sort(char_vec)
print(sorted_char_vec)
# Sorting a data frame by one column
df_unsorted <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(30, 25, 35),
Score = c(85, 92, 78)
)
# Using order() for base R sorting
df_sorted_age <- df_unsorted[order(df_unsorted$Age), ]
print(df_sorted_age)
# Using dplyr::arrange() for tidyverse sorting
# install.packages("dplyr") # if not already installed
library(dplyr)
df_sorted_score <- df_unsorted %>% arrange(Score)
print(df_sorted_score)
# Sorting by multiple columns (Age ascending, then Score descending)
df_multi_sorted <- df_unsorted %>% arrange(Age, desc(Score))
print(df_multi_sorted)
Examples of sorting vectors and data frames
order()
function returns the indices that would sort the vector, which is useful for reordering other vectors or rows in a data frame. sort()
directly returns the sorted vector.Practical Application: Ordered Bar Charts
A common use case for reordering factor levels is to create bar charts where categories are ordered by their corresponding values, rather than alphabetically. This makes the chart much easier to interpret.
# Sample data
product_sales <- data.frame(
Product = c("A", "B", "C", "D", "E"),
Sales = c(150, 200, 120, 300, 180)
)
# Reorder Product factor by Sales (descending)
product_sales$Product <- fct_reorder(product_sales$Product, product_sales$Sales, .desc = TRUE)
# Plotting with ggplot2
# install.packages("ggplot2") # if not already installed
library(ggplot2)
ggplot(product_sales, aes(x = Product, y = Sales)) +
geom_bar(stat = "identity", fill = "steelblue") +
labs(title = "Product Sales (Ordered)", x = "Product", y = "Total Sales") +
theme_minimal()
Creating an ordered bar chart using fct_reorder
and ggplot2