How to Create 3D Pie Charts?

Mounish V Last Updated : 18 Jun, 2024
3 min read

Introduction

Data visualization is an important step toward discovering insights and patterns. Among the various tools at our disposal are charts, which explain complicated information simply and straightforwardly. The 3D pie chart is a very handy graphic. Traditional pie charts demonstrate how different categories contribute to a whole, while 3D pie charts offer depth and character. These charts, which use 3D effects, can help demonstrate complicated relationships across categories more naturally and interestingly. We’ll present 3D pie charts and utilize RStudio.

Overview

  • Understand the need for 3D pie charts.
  • Learn about a new library ‘plotrix’
  • Learn how these pie charts can be customized.
  • Learn the syntax in RStudio.

3D Pie Charts Using RStudio

The plotrix package is a collection of plotting functions for R, primarily focused on enhancing the capabilities of base graphics. It includes functions for creating various plots and adding special effects like 3D effects to plots. Some key features of Plotrix include:

  • Specialized Plotting: This program provides functions for creating complex plots, such as 3D pie charts (pie3D), 3D scatter plots, and polar plots.
  • Enhanced Customization: Offers additional parameters to customize plots beyond what is available in base R graphics.
  • Compatibility: Integrates smoothly with base R graphics and other plotting packages, allowing users to combine functionalities as needed.
  • Documentation: Comprehensive documentation and examples are available to help users understand and utilize the functions effectively.

Also Read: Types of Plots: Visualization from Concept to Code

Parameters of ‘pie3D’

We’ll use pie3D() from plotrix to create 3D pie charts in RStudio.

  • x: The data for the pie chart, typically a vector of numeric values representing the sizes of the slices.
  • labels: Labels for each slice of the pie chart. In this example, we combine the cylinder counts with percentages using paste().
  • explode: Numeric vector specifying how much each slice should be separated from the center. A value of 0 means no separation.
  • col: Vector of colors for the slices. Each color corresponds to a segment.
  • main: Title for the pie chart.
  • labelcex: Character expansion factor for labels. Adjusts the size of the labels relative to the default.
  • labelrad: Radial position of labels. Adjusts how far the labels are positioned from the center of the pie.
  • theta: Viewing angle. Controls the angle of rotation for the 3D effect.

How to Create 3D Pie Charts?

First, install let us install the plotrix library:

install.packages("plotrix")

We’ll be using the mtcars dataset in R, which contains data on various car models, including details like cylinders, horsepower, and miles per gallon (mpg).

# Load necessary packages

library(plotrix)

# Load the mtcars dataset

data(mtcars)

# Count the number of cars for each cylinder count

cylinder_counts <- table(mtcars$cyl)

# Calculate percentages

percentages <- round(100 * cylinder_counts / sum(cylinder_counts), 1)

# Labels for the pie chart with percentages

labels <- paste(names(cylinder_counts), "cylinders\n", percentages, "%", sep = "")

# Custom colors for each segment

colors <- c("#E74C3C", "#3498DB", "#2ECC71")  # Red, Blue, Green

# Create 3D pie chart

pie3D(cylinder_counts, labels = labels, explode = 0.1, col = colors,

      main = "Distribution of Cars by Cylinder Count",

      labelcex = 0.8, labelrad = 1.2, theta = 0.1)
How to Create 3D Pie Charts?

Conclusion

The chart showed us a visually appealing 3D chart of the distribution of cars by cylinder count. We can see that 8 cylinders are the majority. Charts are powerful tools for visualizing data, providing a clear and engaging way to depict complex information. Using the plotrix package in RStudio, creating these charts becomes straightforward. The parameters can also help customize the charts according to one’s liking.

Frequently Asked Questions

Q1. How to install the plotrix package?

A. You can install the plotrix package from CRAN using the following command: install.packages(“plotrix”)

Q2. How to load the plotrix package in my RStudio?

A. After installing the package, load it into your R session with: library(plotrix)

Q3. How do I change the angle of the 3D pie chart for better visualization?

A. You can change the angle using the theta argument: pie3D(slices, labels = labels, col = rainbow(length(slices)), theta = 0.8, main = “3D Pie Chart with Angle Change”)

Q4. How to save a 3D pie chart to a file?

A. You can save your chart using functions like png, jpeg, or pdf. For example: png(“3D_pie_chart.png”)
   pie3D(slices, labels = labels, col = rainbow(length(slices)), main = “3D Pie Chart Example”)
   dev.off()

I'm a tech enthusiast, graduated from Vellore Institute of Technology. I'm working as a Data Science Trainee right now. I am very much interested in Deep Learning and Generative AI.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details