Skip to content

R - Pie Charts

R - Modern Charting: Why to Avoid Pie Charts and What to Use Instead

Section titled “R - Modern Charting: Why to Avoid Pie Charts and What to Use Instead”

Pie charts are a familiar way to show parts of a whole. However, in the field of data visualization, they are often discouraged. Why? Because humans are not good at accurately comparing angles. It’s much easier for us to compare lengths, making bar charts a more effective and honest way to display proportional data.

This tutorial will first show you the modern way to create a pie chart using the powerful ggplot2 library. Then, it will demonstrate why a simple bar chart is often a superior alternative.

Forget base R’s pie() function. ggplot2, the cornerstone of the tidyverse, provides a flexible and powerful grammar of graphics. We don’t create a pie chart directly; instead, we create a bar chart and then transform its coordinate system.

# Install ggplot2 if you haven't already
# install.packages("tidyverse")
library(ggplot2)
library(dplyr)

ggplot2 works best with data in a data frame (or tibble).

# Create a tibble for our data
city_data <- tibble(
city = c("London", "New York", "Singapore", "Mumbai"),
market_share = c(21, 62, 10, 53)
)

The foundation of a ggplot2 pie chart is a stacked bar chart where we map our values to the y aesthetic.

bar_plot <- ggplot(city_data, aes(x = "", y = market_share, fill = city)) +
geom_col(width = 1)
# This creates a single stacked bar. Now we transform it.

Step 3: Convert to a Pie Chart with coord_polar()

Section titled “Step 3: Convert to a Pie Chart with coord_polar()”

The magic happens with coord_polar(), which wraps the bar chart into a circle.

pie_chart <- bar_plot +
coord_polar(theta = "y") +
labs(
title = "Market Share by City",
fill = "City",
x = NULL, # Remove x-axis label
y = NULL # Remove y-axis label
) +
theme_minimal() + # Use a clean theme
theme(axis.text = element_blank(), axis.ticks = element_blank()) # Remove axis text
print(pie_chart)

Description of Output: This code produces a clean, modern-looking pie chart. The slices are colored according to the city, and a legend is automatically generated. The title is displayed at the top. The background is minimal, with no distracting grid lines or axis labels.

Adding labels to a pie chart is crucial for readability. We first calculate the percentages using dplyr.

# Calculate percentages and label positions
city_data <- city_data %>%
mutate(
percentage = market_share / sum(market_share),
label = scales::percent(percentage)
)
# Rebuild the plot and add a geom_text layer
ggplot(city_data, aes(x = "", y = percentage, fill = city)) +
geom_col(width = 1) +
coord_polar(theta = "y") +
# Add labels inside the slices
geom_text(
aes(label = label),
position = position_stack(vjust = 0.5) # Center the labels
) +
labs(title = "Market Share by City", fill = "City", x = NULL, y = NULL) +
theme_void() # A theme perfect for pie charts

Now, let’s create a bar chart with the same data. Notice how much easier it is to compare New York (62) and Mumbai (53). In the pie chart, this comparison is difficult.

# Use reorder() to sort the bars for easier comparison
ggplot(city_data, aes(x = reorder(city, -market_share), y = market_share, fill = city)) +
geom_col() +
geom_text(aes(label = market_share), vjust = -0.5) + # Add value labels
labs(
title = "Market Share by City",
x = "City",
y = "Market Share (Units)"
) +
theme_minimal() +
theme(legend.position = "none") # Remove legend as fill is redundant

Conclusion: While you can make pie charts in R, you should always consider if a bar chart would communicate your message more effectively. Use pie charts sparingly, and only for a small number of categories that sum to a meaningful whole.

3D pie charts are strongly discouraged in data science and statistics. The 3D perspective distorts the size of the slices, making them misleading. The slices in the foreground appear larger than equally sized slices in the background. For this reason, ggplot2 does not provide a built-in way to create them, reinforcing modern visualization best practices. Always prefer a 2D representation.