In this post, we will learn how to make simple barplot using ggplot2 and learn to reorder barplots in R. We will make a barplot using ggplot2’s geom_col() with multiple bars. By default, ggplot2 orders the bars in alphabetical order. We will see examples of reordering barplots by another numerical variable in the data using forcats’ fct_reorder() function and reorder() function in base R.
Let us load the packages needed.
library(tidyverse) theme_set(theme_bw(16))
We will use the diamonds dataset that is built-in in R.
diamonds %>% head() # A tibble: 6 × 10 carat cut color clarity depth table price x y z <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
We will make a barplot between cut and average diamond price.
df <-diamonds %>% group_by(cut) %>% summarize(avg_price = mean(price)) df # A tibble: 5 × 2 cut avg_price <ord> <dbl> 1 Fair 4359. 2 Good 3929. 3 Very Good 3982. 4 Premium 4584. 5 Ideal 3458.
When we make a simple barplot between categorical variable and numerical variable using geom_col() function ggplot2.
df %>% ggplot(aes(x = cut, y = avg_price))+ geom_col() ggsave("Barplot_with_geom_col_ggplot2.png")
Note by default, ggplot2 orders the bars based on the levels of the categorical variable. In our example, cut variable is an ordinal variable and we can find the order using levels() function.
levels(df$cut) [1] "Fair" "Good" "Very Good" "Premium" "Ideal"
Reordering Bars with reorder() function
We can order the bars in the barplot based on the value of average price using base R’s reorder() function as shown below.
df %>% ggplot(aes(x=reorder(cut, avg_price), y=avg_price))+ geom_col() ggsave("reorder_barplots_with_base_R_reorder_ggplot2.png")
Fixing axis label after using reorder() function
We have barplot nicely ordered from low to high price of diamonds. However, our x-axis label is not what we want. We can change the axis label with labs() function in ggplot2.
df %>% ggplot(aes(x=reorder(cut, avg_price), y=avg_price))+ geom_col()+ labs(x="cut") ggsave("reorder_barplots_with_reorder_ggplot2.png")
Reordering bars in barplot using fct_reorder() function
Another way we can reorder the bars in barplot is to use fct_reorder() function in forcats packages. It works the same way as reorder() function.
df %>% ggplot(aes(x=fct_reorder(cut, avg_price), y=avg_price))+ geom_col()+ labs(x="cut") ggsave("reorder_barplots_with_fct_reorder_ggplot2.png")