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:
| Product | Sales |
|---|---|
| A | 20 |
| B | 30 |
| C | 25 |
| D | 25 |
sales <- c(20,30,25,25)
pie(sales)
This produces a pie chart with four sectors.
Example 2: Adding Labels
Example 3: Adding Title
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:
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:
Example 7: Clockwise Pie Chart
Example 8: Changing Radius
sales <- c(20,30,25,25)
pie(sales,
radius=0.8)
Example 9: Grade Distribution
Example 10: Market Share Analysis
Suppose market shares are:
| Company | Share |
|---|---|
| Apple | 35 |
| Samsung | 30 |
| Xiaomi | 20 |
| Others | 15 |
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
Example 13: Budget Allocation
Example 14: Using Data Frames
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
Important Parameters
| Parameter | Purpose |
|---|---|
| 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 Plot | Pie 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
Post a Comment