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

library(plotrix)

sales <- c(20,30,25,25)

products <- c("A","B","C","D")

pie3D(sales,
labels=products)




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

library(plotrix)

sales <- c(20,30,25,25)

products <- c("A","B","C","D")

pie3D(sales,
labels=products,
col=rainbow(4),
main="Product Sales Distribution")




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

library(plotrix)

share <- c(35,30,20,15)

company <- c("Apple",
"Samsung",
"Xiaomi",
"Others")

pie3D(share,
labels=company,
explode=0.1,
col=rainbow(4),
main="Market Share")




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

ParameterPurpose
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:

library(plotly)

labels <- c("Product A", "Product B", "Product C", "Product D")
values <- c(20, 30, 25, 25)

fig <- plot_ly(
  labels = labels,
  values = values,
  type = 'pie',
  pull = c(0, 0.2, 0, 0)
)

fig





Summary

FunctionPackagePurpose
pie()Base R2D Pie Chart
pie3D()plotrix3D Pie Chart
plot_ly()plotlyInteractive Pie Chart

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1