Use cases of the reorder_within function in R

infoart.ca
3 min readOct 10, 2024

--

The reorder_within function provides a powerful way to reorganize categorical data for more insightful visualizations in R. This function is part of the tidytext package that allows the reordering of factors within a group or facet, which is essential when dealing with faceted plots to ensure that each facet is ordered independently.

Let’s explore the function visually:

Using the mtcars dataset, we will group and summarize average hp by gear and cyl count, and reorder the cyl factor within each gear group.

 library(ggplot2)
library(dplyr)
library(tidytext)

# Prepare the data
mtcars_hp <- mtcars %>%
group_by(gear, cyl) %>%
summarise(avg_hp = mean(hp)) %>%
ungroup() %>%
mutate(cyl = reorder_within(cyl, avg_hp, gear))

# Plot with reordering within facets and free scales for both x and y axes
ggplot(mtcars_hp, aes(x = cyl, y = avg_hp)) +
geom_bar(stat = "identity") +
scale_x_reordered() +
facet_wrap(~ gear, scales = "free") +
labs(x = "Number of Cylinders",
y = "Average Horsepower")

Here is the output:

We will do another chart using the gapminder data. Similar to our previous one, we will calculate the average life expectancy for each country, and order them within their corresponding continents.


gapminder_summary <- gapminder %>%
group_by(continent, country) %>%
summarise(avg_lifeExp = mean(lifeExp)) %>%
ungroup() %>%
mutate(country = reorder_within(country, avg_lifeExp, continent))

ggplot(gapminder_summary, aes(x = country, y = avg_lifeExp)) +
geom_bar(stat = "identity") +
scale_x_reordered() +
facet_wrap(~ continent, scales = "free") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(title = "Average Life Expectancy by Country and Continent",
x = "Country",
y = "Average Life Expectancy")

And here is the plot:

The main work is complete. We can now focus on improving the aesthetics of the plot to improve readability and visual appeal…

 ggplot(gapminder_summary, aes(x = country, y = avg_lifeExp)) +
geom_segment(aes(xend = country, yend = 0), color = "#6C757D") +
geom_point(aes(color = continent), size = 3) +
scale_color_manual(values = c(
"Africa" = "#FF5733",
"Americas" = "#33FF57",
"Asia" = "#3357FF",
"Europe" = "#FF33A1",
"Oceania" = "#FFD700"
), name = NULL) +
scale_x_reordered() +
facet_wrap(~ continent, scales = "free") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 90, hjust = 1, size = 8),
axis.text.y = element_text(size = 8),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "bottom",
legend.title = element_blank(),
legend.text = element_text(size = 8)
) +
labs(title = "Average Life Expectancy by Country and Continent",
x = "Country",
y = "Average Life Expectancy",
caption = "Legend") +
theme(plot.caption = element_text(hjust = 0.5))

Happy reordering within…

--

--

infoart.ca
infoart.ca

Written by infoart.ca

Center for Social Capital & Environmental Research | Posts by Bishwajit Ghose, BI consultant and lecturer at the University of Ottawa

No responses yet