Introduction
Creating a pie chart in R is one of the most straightforward ways to visualize categorical data and show how each category contributes to the whole. Still, whether you are working with the base graphics system or the more flexible ggplot2 package, R provides powerful tools to generate clear, publication‑ready circle charts. This guide walks you through the complete process, from data preparation to customization, and includes practical tips, scientific insight, and common troubleshooting steps. By the end of this article you will be able to create a pie chart in R confidently for any dataset, using both the simple pie() function and the more versatile ggplot2 approach Easy to understand, harder to ignore..
Steps to Create a Pie Chart in Base R
Step 1: Prepare Your Data
The foundation of any successful visualization starts with well‑structured data. For a pie chart you typically need a vector of values representing each slice’s size and an optional label vector.
# Example data
values <- c(30, 45, 25) # numeric values for each slice
labels <- c("Category A", "Category B", "Category C")
If your data comes from a data frame, you can extract the relevant columns:
df <- data.frame(
category = c("A", "B", "C"),
amount = c(30, 45, 25)
)
values <- df$amount
labels <- df$category
Step 2: Use the pie() Function
The base R function pie() draws the circle automatically. The simplest call looks like this:
pie(values, labels = labels, main = "Sales by Category")
valuesdefines the size of each slice.labelsprovides text for each slice (optional).mainsets the chart title.
Step 3: Customize Colors and Labels
You can control the appearance by passing additional arguments:
pie(values,
labels = labels,
main = "Sales Distribution",
col = c("#FF6384", "#36A2EB", "#FFCE56"), # slice colors
legend = TRUE,
border = "white") # slice border color
colaccepts a vector of hex colors or named colors.legend = TRUEadds a legend that mirrors the slice colors.bordersets the outline of each slice.
Step 4: Add Title, Subtitle, and Adjust Layout
For a more polished look, you can add a subtitle using mtext() and adjust the overall plot region with par():
par(mar = c(4, 4, 2, 2) + 0.1) # increase bottom margin for legend
pie(values,
labels = labels,
main = "Sales Distribution",
col = c("#FF6384", "#36A2EB", "#FFCE56"),
legend = TRUE,
border = "white")
mtext("Figure 1: Quarterly sales breakdown", side = 3, line = -1, font = 2)
par(mar = ...)tweaks the plot margins so the legend does not overlap the chart.mtext()places a subtitle above the main title.
Creating a Pie Chart with ggplot2
While the base pie() function is quick, ggplot2 offers greater flexibility for layering, theming, and integration with the tidyverse workflow That's the part that actually makes a difference..
Step 1: Install and Load ggplot2
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
library(ggplot2)
Step 2: Prepare Data in Tidy Format
ggplot2 works best when your data is in “tidy” format—each row represents a single observation. For a pie chart you usually need two columns: a categorical variable and a numeric value Worth keeping that in mind..
library(tidyverse)
df <- tibble(
category = c("A", "B", "C"),
amount = c(30, 45, 25)
)
Step 3: Use geom_bar() with coord_polar()
The classic method to produce a pie chart in ggplot2 is to draw a stacked bar chart and then wrap it around a circle:
ggplot(df, aes(x = "", y = amount, fill = category)) +
geom_bar(stat = "identity", width = 1, color = "white", size = 0.5) +
coord_polar("y", start = 0) +
theme_void() +
scale_fill_manual(values = c("#FF6384", "#36A2EB", "#FFCE56"))
aes(x = "", y = amount, fill = category)creates an empty x‑axis so the bars are drawn as slices.coord_polar("y", start = 0)transforms the y‑axis into a circle.theme_void()removes axes and grid lines for a clean pie look.
Step 4: Customize Aesthetics
You can enhance the chart by adding a title, labels, and a legend:
ggplot(df, aes(x = "", y = amount, fill = category)) +
geom_bar(stat = "identity", width = 1, color = "white", size = 0.5) +
coord_polar("y", start = 0) +
labs(title = "Sales Distribution",
fill = "Category") +
theme_minimal() +
scale_fill_manual(values = c("#FF6384", "#36A2EB", "#FFCE56"))
labs()sets the chart title and legend title.theme_minimal()provides a subtle background; you can replace it withtheme_void()for a pure pie appearance.
Step 5: Add Percentages or Values on Slices
To make the chart more informative, calculate the percentage for each slice and annotate it:
df <- df %>%
mutate(percent = round(amount / sum(amount) * 100, 1))
ggplot(df, aes(x = "", y = amount, fill = category)) +
geom_bar(stat = "identity", width = 1, color = "white", size = 0.5) +