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

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

Simple Barpots and reordering bars in R

rstats101 · September 17, 2023 ·

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"    
How to reorder barplots in R
How to reorder barplots in R

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")

Reorder barplot by a variable using reorder()
Reorder barplot by a variable using reorder()

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")

Fixing axis labels after reorder() function
Fixing axis labels after reorder() function

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")

Reorder barplot by a variable using fct_reorder()
Reorder barplot by a variable using fct_reorder()

Related

Filed Under: rstats101 Tagged With: barplots in R, reorder barplots with reorder()

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

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

Go to mobile version