Ordered bar chart with R

Cedric Vidonne

Lei Chen

Ordered bar chart with R

An ordered bar chart is a chart in which each category is represented by a horizontal rectangle, with the length of the rectangle being ordered and proportional to the values being plotted.

More about: Ordered bar chart


Ordered bar chart

# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(scales)

# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/ranking/bar_ordered.csv")

# Plot
ggplot(df) +
  geom_col(aes(
    x = returnee_number,
    y = reorder(country_origin, returnee_number)
  ),
  fill = unhcr_pal(n = 1, "pal_blue"),
  width = 0.8
  ) +
  labs(
    title = "Refugee returns by country of origin (top 5) | 2020",
    x = "Number of people",
    caption = "Source: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    expand = expansion(c(0, 0.1)),
    breaks = pretty_breaks(),
    labels = label_number_si()
  ) +
  theme_unhcr(
    grid = "X",
    axis = "y",
    axis_title = "x"
  )

An ordered bar showing refugee returns by country of origin (top 5) | 2020


Ordered bar chart with labels

# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(scales)

# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/ranking/bar_ordered.csv")

# Plot
ggplot(df) +
  geom_col(aes(
    x = returnee_number,
    y = reorder(country_origin, returnee_number)
  ),
  fill = unhcr_pal(n = 1, "pal_blue"),
  width = 0.8
  ) +
  geom_text(aes(
    x = returnee_number,
    y = reorder(country_origin, returnee_number),
    label = format(returnee_number, big.mark = ",", scientific = FALSE)
  ),
  hjust = -0.1,
  size = 8 / .pt
  ) +
  labs(
    title = "Refugee returns by country of origin (top 5) | 2020",
    subtitle = "Number of people",
    caption = "Source: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    expand = expansion(c(0, 0.1)),
    limits = c(NA, 140000)
  ) +
  theme_unhcr(
    grid = FALSE,
    axis = "y",
    axis_title = FALSE,
    axis_text = "y"
  )

An ordered bar showing refugee returns by country of origin (top 5) | 2020


Related chart with R