Pie chart with R

Cedric Vidonne

Lei Chen

Pie chart with R

A pie chart shows how a total amount is divided between different categorical variables as a circle divided into proportional segments. Each categorical value corresponds with a single slice of the circle, and each arc length indicates the proportion of each category.

More about: Pie chart


Pie 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/pie.csv")

# Plot
ggplot(df) +
  geom_arc_bar(aes(
    x0 = 0,
    y0 = 0,
    r0 = 0,
    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
  ) +
  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 pie chart showing UNHCR Funding (as of February 2022)


Related chart with R