Table of Contents
Boxplots are graphical representations of data that display the distribution, spread, and central tendencies of a dataset. In R, boxplots can be reordered to better understand the data or to highlight specific patterns or trends. This can be done by specifying the order of the variables or categories in the dataset before creating the boxplot. For example, if we have a dataset with three categories (A, B, C) and we want the boxplot to display the categories in the order of B, A, C, we can use the “reorder” function in R. This will reorder the categories in the dataset accordingly and the resulting boxplot will reflect the new order. Other functions such as “order” and “factor” can also be used for reordering boxplots in R.
Reorder Boxplots in R (With Examples)
Often you may want to reorder boxplots in R.
The following examples show how to do so using two different methods:
- Method 1: Reorder Based on Specific Order
- Method 2: Reorder Based on Median Value of Boxplot
Each example will use the built-in airquality dataset in R:
#view first six lines of airquality data
head(airquality)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
Here’s what a plot of multiple boxplots will look like for this dataset without specifying an order:
#create boxplot that shows distribution of temperature by month
boxplot(Temp~Month, data=airquality, col="lightblue", border="black") 
Example 1: Reorder Boxplots Based on Specific Order
The following code shows how to order the boxplots based on the following order for the Month variable: 5, 8, 6, 9, 7.
#reorder Month values
airquality$Month <- factor(airquality$Month , levels=c(5, 8, 6, 9, 7))
#create boxplot of temperatures by month using the order we specified
boxplot(Temp~Month, data=airquality, col="lightblue", border="black")

Notice that the boxplots now appear in the order that we specified using the levels argument.
Related:
Example 2: Reorder Boxplots Based on Median Value
The following code shows how to order the boxplots in ascending order based on the median temperature value for each month:
#reorder Month values in ascending order based on median value of Temp
airquality$Month <- with(airquality, reorder(Month , Temp, median , na.rm=T))
#create boxplot of temperatures by month
boxplot(Temp~Month, data=airquality, col="lightblue", border="black")
The boxplots now appear in ascending order based on the median value for each month.
Note: The median value for each boxplot is the horizontal black line that runs through the middle of each box.
We can also order the boxplots in descending order by using a negative sign in front of Temp in the reorder function:
#reorder Month values in descending order based on median value of Temp
airquality$Month <- with(airquality, reorder(Month , -Temp, median , na.rm=T))
#create boxplot of temperatures by month
boxplot(Temp~Month, data=airquality, col="lightblue", border="black")
The boxplots now appear in descending order based on the median value for each month.
Additional Resources
Cite this article
stats writer (2024). How can boxplots be reordered in R, and can you provide examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-boxplots-be-reordered-in-r-and-can-you-provide-examples/
stats writer. "How can boxplots be reordered in R, and can you provide examples?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-boxplots-be-reordered-in-r-and-can-you-provide-examples/.
stats writer. "How can boxplots be reordered in R, and can you provide examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-boxplots-be-reordered-in-r-and-can-you-provide-examples/.
stats writer (2024) 'How can boxplots be reordered in R, and can you provide examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-boxplots-be-reordered-in-r-and-can-you-provide-examples/.
[1] stats writer, "How can boxplots be reordered in R, and can you provide examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can boxplots be reordered in R, and can you provide examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
