3D Pie Chart in R
Generating a 3D Pie Chart in R
The base R function pie() generates only 2D pie charts. To create 3D pie charts, we use the plotrix package, which provides the pie3D() function.
Step 1: Install the Package
install.packages("plotrix")
Load the package:
library(plotrix)
Syntax
pie3D(x,
labels,
explode,
main,
col)
where
- x : vector of values
- labels : labels for sectors
- explode : distance to separate slices
- main : title
- col : colors
Example 1: Simple 3D Pie Chart
library(plotrix)
sales <- c(20,30,25,25)
pie3D(sales)
This produces a simple three-dimensional pie chart.
Example 2: Adding Labels
Example 3: Adding Colors
library(plotrix)
sales <- c(20,30,25,25)
products <- c("A","B","C","D")
pie3D(sales,
labels=products,
col=c("red","blue","green","yellow"))
Example 4: Adding Title
Example 5: Exploding Slices
The explode parameter separates slices from the pie.
library(plotrix)
sales <- c(20,30,25,25)
products <- c("A","B","C","D")
pie3D(sales,
labels=products,
explode=0.1,
col=rainbow(4),
main="3D Pie Chart")
All slices are moved slightly outward.
Example 6: Market Share Analysis
Example 7: Grade Distribution
library(plotrix)
grades <- c(40,30,20,10)
labels <- c("A","B","C","D")
pie3D(grades,
labels=labels,
col=c("green","blue","yellow","red"),
explode=0.1,
main="Grade Distribution")
Example 9: Department-wise Students
library(plotrix)
students <- c(60,40,30,20)
dept <- c("CSE","ECE","ME","CE")
pie3D(students,
labels=dept,
col=rainbow(4),
explode=0.05,
main="Department Strength")
Example 10: Showing Percentages
library(plotrix)
sales <- c(20,30,25,25)
products <- c("A","B","C","D")
percent <- round(sales/sum(sales)*100)
labels <- paste(products,percent,"%")
pie3D(sales,
labels=labels,
explode=0.1,
col=rainbow(4),
main="Sales Distribution")
Important Parameters
| Parameter | Purpose |
|---|---|
labels | Labels for slices |
col | Colors |
explode | Separate slices |
main | Title |
radius | Radius of pie |
theta | Viewing angle |
labelcex | Label size |
Other Package
The plotly package can create interactive 3D-style pie charts:
Summary
| Function | Package | Purpose |
|---|---|---|
pie() | Base R | 2D Pie Chart |
pie3D() | plotrix | 3D Pie Chart |
plot_ly() | plotly | Interactive Pie Chart |

Comments
Post a Comment