[Go to site: main page, start]

Ordering boxplots in base R



This post is dedicated to boxplot ordering in base R. It describes 3 common use cases of reordering issue with code and explanation.

Boxplot Section Boxplot pitfalls

Reordering category by median


The most common need is to reorder categories by increasing median. It allows to quickly spot what group has the highest value and how categories are ranked.

It is doable using the reorder() function in combination with the with() function as suggested below:

Give a specific order


Boxplot categories are provided in a column of the input data frame. This column needs to be a factor, and has several levels. Categories are displayed on the chart following the order of this factor, often in alphabetical order.

Sometimes, we need to show groups in a specific order (A,D,C,B here). This can be done by reordering the levels, using the factor() function.

Grouped and ordered boxplot


In a grouped boxplot, categories are organized in groups and subgroups. For instance, let’s take several varieties (group) that are grown in high or low temperature (subgroup).

Here both subgroups are represented one beside each other, and groups are ranked by increasing median:

Related chart types


Violin
Density
Histogram
Boxplot
Ridgeline



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥