R Pie Charts

Introduction

In this chapter, you will learn how to create and customize pie charts in R using the base R pie() function. Pie charts are circular charts divided into sectors, illustrating numerical proportions. They are useful for displaying the composition of categorical data. Understanding how to create and customize pie charts can help effectively present data insights.

Components of a Pie Chart

A pie chart consists of the following components:

  • Sectors (Slices): Each slice represents a category and its size corresponds to its proportion of the total.
  • Labels: Text labels that describe each slice.
  • Colors: Different colors can be used to differentiate the slices.
  • Title: A main title that describes the chart.
  • Legend: An optional legend to describe each color/slice.

Creating Pie Charts with Base R

The base R pie() function allows you to create simple pie charts quickly.

Basic Pie Chart

To create a basic pie chart, you need a vector of values representing the sizes of the slices and a vector of labels for each slice.

Example:

# Creating a basic pie chart
slices <- c(10, 20, 30, 40)
labels <- c("A", "B", "C", "D")
pie(slices, labels, main = "Basic Pie Chart")

Customizing Pie Charts

You can customize pie charts by adding colors, labels, and other graphical parameters.

Adding Colors

Use the col parameter to specify colors for each slice.

Example:

# Customizing a pie chart with colors
colors <- c("red", "blue", "green", "yellow")
pie(slices, labels, col = colors, main = "Customized Pie Chart")

Adding Percentage Labels

Calculate the percentage for each slice and append it to the labels.

Example:

# Adding percentage labels
percentages <- round(slices / sum(slices) * 100)
labels_with_percentages <- paste(labels, percentages, "%", sep = " ")
pie(slices, labels = labels_with_percentages, col = colors, main = "Pie Chart with Percentages")

Adding a Legend

Use the legend() function to add a legend to the pie chart.

Example:

# Adding a legend
pie(slices, labels = labels_with_percentages, col = colors, main = "Pie Chart with Legend")
legend("topright", legend = labels, fill = colors)

Exploding a Slice

Use the radius and init.angle parameters to explode (offset) a slice.

Example:

# Exploding a slice
pie(slices, labels = labels_with_percentages, col = colors, main = "Exploded Pie Chart", radius = 1.1)

Rotating the Pie Chart

Use the clockwise and init.angle parameters to rotate the pie chart.

Example:

# Rotating the pie chart
pie(slices, labels = labels_with_percentages, col = colors, main = "Rotated Pie Chart", clockwise = TRUE, init.angle = 90)

Example Program Using Pie Charts

Here is an example program that demonstrates the creation and customization of pie charts in R using the base pie() function.

# R Program to Demonstrate Pie Charts

# Data for the pie chart
slices <- c(10, 20, 30, 40)
labels <- c("A", "B", "C", "D")

# Basic pie chart
pie(slices, labels, main = "Basic Pie Chart")

# Customizing pie chart with colors
colors <- c("red", "blue", "green", "yellow")
pie(slices, labels, col = colors, main = "Customized Pie Chart")

# Adding percentage labels
percentages <- round(slices / sum(slices) * 100)
labels_with_percentages <- paste(labels, percentages, "%", sep = " ")
pie(slices, labels = labels_with_percentages, col = colors, main = "Pie Chart with Percentages")

# Adding a legend
pie(slices, labels = labels_with_percentages, col = colors, main = "Pie Chart with Legend")
legend("topright", legend = labels, fill = colors)

# Exploding a slice
pie(slices, labels = labels_with_percentages, col = colors, main = "Exploded Pie Chart", radius = 1.1)

# Rotating the pie chart
pie(slices, labels = labels_with_percentages, col = colors, main = "Rotated Pie Chart", clockwise = TRUE, init.angle = 90)

Conclusion

In this chapter, you learned how to create and customize pie charts in R using the base R pie() function. Pie charts are useful for displaying the composition of categorical data and can be customized to improve their readability and aesthetics. By mastering pie charts, you can effectively communicate the proportions of different categories in your data.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top