YouTube video link with explanations for these examples Video link

Video link https://youtu.be/AFll5Auo8wc

Here is a link for subscribing to this YouTube channel. https://www.youtube.com/c/TechAnswers88?s_confirmation=1

How to control the order of the bars in a barchart in ggplot.

Packages used

#install.packages(ggplot2)
#install.packages(dplyr)
#install.packages(ggeasy)
#install.packages(stringr)
#install.packages(patchwork)

library(ggplot2)
library(dplyr)
library(ggeasy)
library(stringr)
library(patchwork)

Create the data set

# We will use  a dataset which comes with the package ggplot.
# view the data frame to see the structure
View(ggplot2::mpg)

df <- mpg%>%
  dplyr::group_by(manufacturer)%>%
  dplyr::mutate(manufacturer = str_to_title(manufacturer))%>%
  tally()

df
## # A tibble: 15 x 2
##    manufacturer     n
##    <chr>        <int>
##  1 Audi            18
##  2 Chevrolet       19
##  3 Dodge           37
##  4 Ford            25
##  5 Honda            9
##  6 Hyundai         14
##  7 Jeep             8
##  8 Land Rover       4
##  9 Lincoln          3
## 10 Mercury          4
## 11 Nissan          13
## 12 Pontiac          5
## 13 Subaru          14
## 14 Toyota          34
## 15 Volkswagen      27

First chart

This chart is without any ordering but you can notice that it is sorted in alphabetic order.

pl <- ggplot(data = df,aes(x= manufacturer, y = n,fill = manufacturer))
pl <- pl + geom_bar(stat = "identity")
pl <- pl + geom_text(aes(label = n), size = 2, vjust = -0.3)
pl <- pl + theme_classic() + scale_fill_viridis_d()
pl <- pl + theme(legend.position = "none")
pl <- pl + ggeasy::easy_rotate_labels(which = "x", angle = 90)
pl <- pl + labs(x = "Car Manufacturers", y= "Number of Models")
pl <- pl + labs(title = "Models from each manufacturer")
pl <- pl + labs(subtitle = "alphabatical order")
pl

Now sort the bars in an ascending order using the value of column used for the y axis.

pl1 <- ggplot(data = df,aes(x= reorder(manufacturer, n), y = n,fill = manufacturer))
pl1 <- pl1 + geom_bar(stat = "identity")
pl1 <- pl1 + geom_text(aes(label = n), size = 2, vjust = -0.3)
pl1 <- pl1 + theme_classic() + scale_fill_viridis_d()
pl1 <- pl1 + theme(legend.position = "none")
pl1 <- pl1 + ggeasy::easy_rotate_labels(which = "x", angle = 90)
pl1 <- pl1 + labs(x = "Car Manufacturers", y= "Number of Models")
pl1 <- pl1 + labs(title = "Models from each manufacturer")
pl1 <- pl1 + labs(subtitle = "Shown in ascending order of  number of models")
pl1

Now sort the bars in an descending order using the value of column used for the y axis.

pl2 <- ggplot(data = df,aes(x= reorder(manufacturer, - n), y = n,fill = manufacturer))
pl2 <- pl2 + geom_bar(stat = "identity")
pl2 <- pl2 + geom_text(aes(label = n), size =  2, vjust = -0.3)
pl2 <- pl2 + theme_classic() + scale_fill_viridis_d()
pl2 <- pl2 + theme(legend.position = "none")
pl2 <- pl2 + ggeasy::easy_rotate_labels(which = "x", angle = 90)
pl2 <- pl2 + labs(x = "Car Manufacturers", y= "Number of Models")
pl2 <-pl2 + labs(title = "Models from each manufacturer")
pl2 <- pl2 + labs(subtitle = "Shown in descending order of  number of models")

pl2

Using the continuous value field to fill the column colours.

pl3 <- ggplot(data = df,aes(x= reorder(manufacturer, - n), y = n,fill = n))
pl3 <- pl3 + geom_bar(stat = "identity")
pl3 <- pl3 + geom_text(aes(label = n), size =  2, vjust = -0.3)
pl3 <- pl3 + theme_classic()
pl3 <- pl3 + theme(legend.position = "none")
pl3 <- pl3 + ggeasy::easy_rotate_labels(which = "x", angle = 90)
pl3 <- pl3 + labs(x = "Car Manufacturers", y= "Number of Models")
pl3 <- pl3 + ggeasy::easy_all_text_color(color =  "blue")
pl3 <-  pl3 + labs(title = "Models from each manufacturer")
pl3 <- pl3 + labs(subtitle = "Shown in descending order of  number of models")

pl3

# See all the charts in a single view

(pl + pl1)/ (pl2  +  pl3)

Here is the link to subscribe to this channel.

<https://www.youtube.com/c/TechAnswers88?s_confirmation=1>