Visualizing Top N Values with Pie Charts Using R's Tidyverse

Creating a Pie Chart with the Top N Values

=====================================================

In this article, we will explore how to create a pie chart that displays only the top n values from your data. We will also go over some common pitfalls and best practices for creating effective pie charts.

Introduction


Pie charts are a popular way to visualize categorical data, but they can be misleading if not used correctly. One common issue with pie charts is that they do not provide a clear indication of the relative size of each category. However, by using the head function and the coord_polar function from the tidyverse, we can create a pie chart that only shows the top n values.

Using the head Function


The head function in R returns the first few rows of a data frame. In this case, we want to use it to get the top 5 values from our dataset.

subject <- c("Dan", "Tom", "Kim", "Sandra", "Bob", "Martha", "Tony", "Kelly", "Arnold", "Amanda")
value <- c(20, 50, 3, 66, 10, 100, 4, 23, 7.4, 44)
df <- data.frame(subject, value)

library(tidyverse)

# Sort the values in descending order
df %>% arrange(desc(value))

# Get the top 5 values using the head function
top_5_values <- df %>% head(5) %>% pull(value)

Creating a Bar Chart with geom_bar


To create a bar chart that represents our pie chart, we can use the geom_bar function from the ggplot2 package.

# Create a data frame with the top 5 values
top_5_values <- df %>% head(5) %>% pull(value)

# Create a new data frame with the subject and value columns
bar_chart_data <- data.frame(subject = df$subject, value = top_5_values)

# Plot the bar chart
ggplot(bar_chart_data, aes(x = "", y = value, fill = subject)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start = 0)

Creating a Pie Chart with coord_polar


To create the pie chart, we can use the coord_polar function from the ggplot2 package. This function allows us to specify the polar coordinate system for our plot.

# Plot the bar chart as before
bar_chart_data <- data.frame(subject = df$subject, value = top_5_values)

# Create a pie chart using coord_polar
ggplot(bar_chart_data, aes(x = "", y = value, fill = subject)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y", start = 0) +
  theme_void()

Adding Labels to the Pie Chart


To add labels to our pie chart, we can use the geom_text function from the ggplot2 package.

# Plot the bar chart as before
bar_chart_data <- data.frame(subject = df$subject, value = top_5_values)

# Create a pie chart using coord_polar
ggplot(bar_chart_data, aes(x = "", y = value, fill = subject)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y", start = 0) +
  theme_void() +
  
  # Add labels to the pie chart using geom_text
  geom_text(aes(label = paste(subject, ": ", value)),
            position = position_stack(vjust = .5)) +
  scale_fill_manual(values = rainbow(33))

Best Practices for Creating Effective Pie Charts


Here are some best practices to keep in mind when creating effective pie charts:

  • Use relative sizes: Instead of using absolute values, use relative sizes to compare the size of each category.
  • Avoid using 3D effects: Three-dimensional pie charts can be misleading and make it difficult to read the labels.
  • Use a limited number of categories: Too many categories in a pie chart can make it difficult to understand the data. Use only the top n values, as shown in this example.

Conclusion


Creating a pie chart that displays only the top n values is easier than you might think. By using the head function and the coord_polar function from the tidyverse, we can create an effective pie chart that provides clear insight into our data.


Last modified on 2025-01-26