Donut chart with R

Cedric Vidonne

Lei Chen

Donut chart with R

The donut chart is a variation of a pie charts, with the total amount divided into categories based on a proportional value. For the most part, there aren’t significant differences between a pie chart and a donut chart, so the choice of a donut over a standard circle is mostly aesthetic.

More about: Donut chart


Donut chart

# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(ggtext)
library(ggforce)

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

# Plot
ggplot(df) +
  geom_arc_bar(aes(
    x0 = 0,
    y0 = 0,
    r0 = 0.6,
    r = 1,
    amount = funding_value,
    fill = funding_type
  ),
  stat = "pie",
  size = 1,
  color = "#FFFFFF"
  ) +
  geom_richtext(
    x = c(1.25, -1.25),
    y = c(-0.3, 0.3),
    aes(label = paste0(
      funding_type,
      "<br><strong>US$ ",
      round(funding_value / 1e9, 2),
      "B</strong>", "<br>",
      round(100 * funding_value / sum(funding_value), 1),
      "%"
    )),
    size = 8 / .pt,
    fill = NA,
    label.color = NA
  ) +
  geom_richtext(
    x = 0,
    y = 0,
    label = paste0(
      "Total required",
      "<br><strong>US$ ",
      round(sum(df$funding_value) / 1e9, 2),
      "B</strong>"
    ),
    size = 12 / .pt,
    fill = NA,
    label.color = NA
  ) +
  labs(
    title = "2021 UNHCR's funding",
    caption = "Source: Define source here\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_fill_unhcr_d(
    palette = "pal_unhcr",
    direction = -1
  ) +
  scale_x_continuous(expand = expansion(c(0.3, 0.5))) +
  theme_unhcr(
    grid = FALSE,
    axis = FALSE,
    axis_title = FALSE,
    axis_text = FALSE,
    legend = FALSE
  )

A donut chart showing UNHCR Funding (as of February 2022)


Related chart with R