Area chart with R

Cedric Vidonne

Lei Chen

Area chart with R

An area chart, like a line chart, displays the evolution of numeric variables over a continuous period of time. However, in an area chart, the area between the line and x-axis is filled with colour or texture.

More about: Area chart


Area 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/change_over_time/area.csv")

# Plot
ggplot(
  filter(
    df,
    population_type == "Refugees"
  ),
  aes(
    x = year,
    y = population_number
  )
) +
  geom_area(
    fill = unhcr_pal(n = 1, "pal_blue"),
    alpha = 0.3
  ) +
  geom_line(
    size = 1,
    color = unhcr_pal(n = 1, "pal_blue")
  ) +
  labs(
    title = "Number of refugees | 1991-2021",
    y = "Number of people",
    caption = "Source: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(breaks = pretty_breaks()) +
  scale_y_continuous(
    expand = expansion(c(0, 0.1)),
    breaks = pretty_breaks(n = 4),
    labels = label_number_si()
  ) +
  theme_unhcr(
    grid = "Y",
    axis = "x",
    axis_title = "y"
  )

An area chart showing number of refugees | 1990-2021


Related chart with R