Stacked area chart with R

Cedric Vidonne

Lei Chen

Stacked area chart with R

As a variation of a simple area chart, a stacked area chart displays the changes of value of multiple data series over a period of time.

More about: Stacked area chart


Stacked area chart

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

# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/change_over_time/area_stacked.csv") |>
  mutate(months = lubridate::my(paste(months, "2022")))

# Plot
ggplot(df) +
  geom_area(aes(
    x = months,
    y = funding_million,
    fill = funding_type
  )) +
  scale_fill_unhcr_d(palette = "pal_unhcr") +
  labs(
    title = "Evolution of funding in West Africa region | 2020",
    y = "USD millions",
    caption = "Source: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_date(
    date_labels = "%b",
    breaks = pretty_breaks(n = 12)
  ) +
  scale_y_continuous(expand = expansion(c(0, 0.1))) +
  theme_unhcr(
    grid = "Y",
    axis = "x",
    axis_title = "y"
  )

A stacked area chart showing evolution of funding in West Africa region | 2020


Related chart with R