19.9 C
London
Friday, August 15, 2025
HomeTidyverse in Rggplot2 in RHow to Make Pie Charts in ggplot2 (With Examples)

How to Make Pie Charts in ggplot2 (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

pie chart is a type of chart that is shaped like a circle and uses slices to represent proportions of a whole.

This tutorial explains how to create and modify pie charts in R using the ggplot2 data visualization library.

How to Make a Basic Pie Chart

The following code shows how to create a basic pie chart for a dataset using ggplot2:

library(ggplot2)

#create data frame
data category" = c('A', 'B', 'C', 'D'),
                   "amount" = c(25, 40, 27, 8))

#create pie chart
ggplot(data, aes(x="", y=amount, fill=category)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) 

ggplot2 pie chart

How to Modify the Appearance of the Pie Chart

The default pie chart in ggplot2 is quite ugly. The simplest way to improve the appearance is to use theme_void(), which removes the background, the grid, and the labels:

ggplot(data, aes(x="", y=amount, fill=category)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) +
  theme_void()

ggplot2 pie chart with no labels

We can further improve the appearance of the chart by adding labels inside the slices:

ggplot(data, aes(x="", y=amount, fill=category)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) +
  geom_text(aes(label = paste0(amount, "%")), position = position_stack(vjust=0.5)) +
  labs(x = NULL, y = NULL, fill = NULL)

Pie chart in ggplot2 with custom labels

We can customize the chart even further by specifying our own hex colors to use for the slices with the scale_fill_manual() argument:

ggplot(data, aes(x="", y=amount, fill=category)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) +
  geom_text(aes(label = paste0(amount, "%")), position = position_stack(vjust=0.5)) +
  labs(x = NULL, y = NULL, fill = NULL) +
  theme_classic() +
  theme(axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank()) +
  scale_fill_manual(values=c("#FF5733", "#75FF33", "#33DBFF", "#BD33FF"))

Scale fill manual ggplot2 pie chart

Tip: Use this Hex Color Picker to find combinations of hex color codes that go well together.

You can also customize the colors of the slices by simply choosing one of the brewer color scales. For example, here’s what the “blues” color scale looks like:

ggplot(data, aes(x="", y=amount, fill=category)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) +
  geom_text(aes(label = paste0(amount, "%")), position = position_stack(vjust=0.5)) +
  labs(x = NULL, y = NULL) +
  theme_classic() +
  theme(axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank()) +
  scale_fill_brewer(palette="Blues")

Scale brewer in ggplot2

Additional Resources

How to Create a Grouped Boxplot in R Using ggplot2
How to Create a Heatmap in R Using ggplot2
How to Create a Gantt Chart in R Using ggplot2

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories