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
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:
Also Read: Types of Plots: Visualization from Concept to Code
We’ll use pie3D() from plotrix to create 3D pie charts in RStudio.
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)
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.
A. You can install the plotrix package from CRAN using the following command: install.packages(“plotrix”)
A. After installing the package, load it into your R session with: library(plotrix)
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”)
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()