• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Reorder Boxplots in R with ggplot2

datavizpyr · January 28, 2020 ·

In this post, we will learn how to re-order boxplots in R with ggplot2. We will make a boxplot using ggplot2 with multiple groups. By default, ggplot2 orders the groups in alphabetical order. We will see multiple examples of reordering boxplots by another variable in the data using reorder() function in base R. We will also see how to overcome a common error due to missing values in the data.

Load Data and tidyverse

We will use NYC flights data set for the year 2013 to make boxplot. We can get the flights data from R pacakge nycflights13.

Let us load tidyverse and nycflights13 package.

library(tidyverse)
library(nycflights13)
theme_set(theme_bw(base_size=16))

The flights data frame contains multiple details about the flights departed from three NYC area airports.

flights %>% colnames()

##  [1] "year"           "month"          "day"            "dep_time"      
##  [5] "sched_dep_time" "dep_delay"      "arr_time"       "sched_arr_time"
##  [9] "arr_delay"      "carrier"        "flight"         "tailnum"       
## [13] "origin"         "dest"           "air_time"       "distance"      
## [17] "hour"           "minute"         "time_hour"

Let us select a few variables from flights dataframe and estimate flight speed from distance and air_time.

flights_speed <- flights %>%
  select(carrier, distance, air_time)%>%
  mutate(speed=distance/air_time)

Default Boxplot with groups in alphabetical order using ggplot2

We will make boxplot of speed for each of airline carrier to understand the relationship between speed and carrier.

flights_speed %>% head()

## # A tibble: 6 x 4
##   carrier distance air_time speed
##   <chr>      <dbl>    <dbl> <dbl>
## 1 UA          1400      227  6.17
## 2 UA          1416      227  6.24
## 3 AA          1089      160  6.81
## 4 B6          1576      183  8.61
## 5 DL           762      116  6.57
## 6 UA           719      150  4.79

We can make boxplot in R with geom_boxplot() function in ggplot2.

flights_speed %>%
  ggplot(aes(x=carrier, y=speed)) +
  geom_boxplot() +
  labs(y="Speed", x="Carrier", subtitle="Speed vs Carrier: nycflight13 data")

We can see that boxplot made by ggplot is ordered in alphabetical order of names the airline carriers. With so many carriers on x-axis it is not easy to identify carriers with higher average speed or lower speed.

Boxplots in R
Boxplots in R with ggplot2

Reordering boxplots using reorder() in R

A better solution is to reorder the boxes of boxplot by median or mean values of speed. In R we can re-order boxplots in multiple ways. In this example, we will use the function reorder() in base R to re-order the boxes. We use reorder() function, when we specify x-axis variable inside the aesthetics function aes(). reorder() function sorts the carriers by mean values of speed by default.

flights_speed %>%
  ggplot(aes(x=reorder(carrier,speed), y=speed)) +
  geom_boxplot() +
  labs(y="Speed", x="Carrier", 
       subtitle="Sorting Boxplots with missing data")

Reordering boxplots in R: Error due to missing values

When we executed the above code chunk, we should have gotten reordered boxplots. Instead we got a boxplot that is till unordered.

Error in Reordering Boxplots with NAs
Error in Reordering Boxplots with NAs in R

The reason is missing data in our flights_speed data frame. We also see the following warning when we made the plot.

>Removed 9430 rows containing non-finite values (stat_boxplot).

We need to specify within reorder() function to remove the data with missing values using na.rm=TRUE.

flights_speed %>%
  ggplot(aes(x=reorder(carrier,speed,na.rm = TRUE), y=speed)) +
  geom_boxplot() +
  labs(y="Speed", x="Carrier", 
       subtitle="Reordering Boxplots after removing missing data")

Now we have reordered boxplot. By default, it is re-ordered in ascending order.

How To Reorder Boxplots in Ascending Order?
How To Sort Boxplots in R?

Reordering boxplots in descending order

To sort boxes in boxplot in descending order, we add negation to speed within reorder() function.

flights_speed %>%
  ggplot(aes(x=reorder(carrier,-speed, na.rm = TRUE), y=speed)) +
  geom_boxplot() +
  labs(y="Speed", x="Carrier", 
       subtitle="Reordering Boxplots: In Descending Order")

Now have reordered boxplots in descending order.

Sorting Boxplots in Descending Order
How To Reorder Boxplots in R? Descending Order with reorder()?

Related posts:

Adjust Boxplot Line Thickness: ggplot2How to Make Boxplots with ggplot2 in R? Adjusting width: Boxplot with points using geom_jitter() with jitterHow To Make Boxplots with Data Points in R using ggplot2? Violinplot of Data from 3 Different DistributionsViolinplot vs Boxplot: Why Violinplot Is Better Than Boxplot Boxplot with line connecting mean values with ggplot2 in RHow to Make Boxplot with a Line Connecting Mean Values in R?

Filed Under: Boxplot ggplot2, R Tagged With: ggplot boxplot, reorder boxplot ggplot, reorder boxplot R

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version