Pie Charts in R


Pie Charts in R

Introduction

A pie chart is a circular statistical graph divided into sectors (slices), where each slice represents a category and its size is proportional to the corresponding value.

Pie charts are useful for showing:

  • Percentage distribution
  • Market share
  • Budget allocation
  • Population distribution
  • Grade distribution
  • Product sales composition

In R, pie charts are created using the pie() function.


Syntax

pie(x,
labels,
main,
col,
clockwise,
radius)

where:

  • x : numeric vector containing values.
  • labels : labels for slices.
  • main : title.
  • col : colors of slices.
  • clockwise : direction of slices.
  • radius : radius of the pie.

Example 1: Simple Pie Chart

Suppose sales of four products are:

ProductSales
A20
B30
C25
D25
sales <- c(20,30,25,25)

pie(sales)

This produces a pie chart with four sectors.




Example 2: Adding Labels

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

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

pie(sales,
labels=products)



Example 3: Adding Title

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

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

pie(sales,
labels=products,
main="Product Sales Distribution")



Example 4: Changing Colors

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

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

pie(sales,
labels=products,
col=c("red","blue","green","yellow"))

Each sector is displayed in a different color.




Example 5: Displaying Percentages

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

percent <- round(sales/sum(sales)*100)

labels <- paste(percent,"%")

pie(sales,
labels=labels,
col=c("red","blue","green","yellow"))

Output labels:

20%
30%
25%
25%




Example 6: Labels with Category and Percentage

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

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

percent <- round(sales/sum(sales)*100)

labels <- paste(products,
percent,
"%")

pie(sales,
labels=labels,col=c("red","blue","green","yellow")

Labels become:

A 20%
B 30%
C 25%
D 25%




Example 7: Clockwise Pie Chart

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

pie(sales,
clockwise=TRUE)



Example 8: Changing Radius

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

pie(sales,
radius=0.8)



Example 9: Grade Distribution

grades <- c(40,30,20,10)

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

pie(grades,
labels=labels,
col=c("green","blue","yellow","red"),
main="Grade Distribution")



Example 10: Market Share Analysis

Suppose market shares are:

Company    Share
Apple    35
Samsung    30
Xiaomi    20
Others    15
share <- c(35,30,20,15)

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

pie(share,
labels=company,
col=rainbow(4),
main="Market Share")




Example 11: Using Rainbow Colors

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

pie(sales,
labels=c("A","B","C","D"),
col=rainbow(4))

rainbow(4) generates four colors automatically.




Example 12: Student Department Distribution

students <- c(60,40,30,20)

dept <- c("CSE",
"ECE",
"ME",
"CE")

pie(students,
labels=dept,
col=rainbow(4),
main="Department-wise Students")



Example 13: Budget Allocation

budget <- c(40,25,20,15)

heads <- c("Salary",
"Maintenance",
"Equipment",
"Misc")

pie(budget,
labels=heads,
col=c("red","green","blue","yellow"),
main="Budget Allocation")





Example 14: Using Data Frames

sales <- data.frame(
Product=c("Laptop",
"Mobile",
"TV",
"Printer"),
Amount=c(120,150,90,60)
)

pie(sales$Amount,
labels=sales$Product,
col=rainbow(4),
main="Sales Distribution")




Example 15: Frequency Distribution Using Factors

grades <- factor(
c("A","B","A","A","C","B","A","D"))

freq <- table(grades)

pie(freq,
col=rainbow(length(freq)),
main="Grade Frequency")

Generating Legend

#To add a list of explanation for each pie, use the legend() function:
# Create a vector of pies
x <- c(10,20,30,40)

# Create a vector of labels
mylabel <- c("2019", "2020", "2021", "2022")
colors <- c("blue", "yellow", "green", "black")

# Display the pie chart with labels
pie(x, label = mylabel,col=colors, main = "Growth")
legend("bottomright", mylabel, fill = colors)





Generating Labels with Percentages

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

percent <- round(sales/sum(sales)*100)

labels <- paste(
c("A","B","C","D"),
percent,
"%"
)

pie(sales,
labels=labels)



Important Parameters

ParameterPurpose
x    Values
labels    Labels for slices
main    Title
col    Colors
clockwise    Direction
radius    Size of pie
init.angle    Starting angle

Useful Functions

Generate Colors

rainbow(n)

Example:

rainbow(5)

Calculate Percentage

percent <- value/sum(value)*100

Create Labels

paste(name,percent,"%")

Applications of Pie Charts

  • Market share analysis
  • Product sales distribution
  • Budget allocation
  • Population composition
  • Student grade distribution
  • Election results
  • Department-wise student strength
  • Expense analysis

Advantages

  • Simple and easy to understand.
  • Suitable for percentage representation.
  • Good for a small number of categories.
  • Effective for part-to-whole comparison.

Limitations

  • Difficult to compare many categories.
  • Not suitable for large datasets.
  • Exact values are difficult to estimate.
  • Bar charts are often better for precise comparisons.

Difference Between Bar Plot and Pie Chart

Bar PlotPie Chart
Compares magnitudes    Shows proportions
Rectangular bars    Circular sectors
Suitable for many categories    Suitable for few categories
Easy to compare values    Easy to visualize percentages
Created using barplot()    Created using pie()

Conclusion

A pie chart is a graphical representation used to show how individual categories contribute to a whole. In R, pie charts are created using the pie() function. By customizing colors, labels, percentages, and titles, informative charts can be created for visualizing distributions and proportions in statistics, business, and data analysis.

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