Data Visualization & Graphics in R
Estimated reading: 4 minutes 29 views

🥧📊 R – Pie Charts and Bar Charts: Create Category Plots with Base R & ggplot2


🧲 Introduction – Visualizing Categorical Data in R

When working with categorical data, two common visualization tools are Pie Charts and Bar Charts. R provides both base plotting and ggplot2 options to create these visuals effectively. While pie charts show proportions, bar charts are better for comparing values across categories.

🎯 In this guide, you’ll learn:

  • How to create pie and bar charts in base R and ggplot2
  • Use color, labels, legends, and customization
  • Choose between bar and pie charts based on best practices
  • Understand chart creation line by line

🥧 1. Pie Charts in Base R

✅ Basic Pie Chart

slices <- c(10, 20, 30, 40)
labels <- c("Q1", "Q2", "Q3", "Q4")
pie(slices, labels = labels, main = "Quarterly Revenue Share")

🔍 Explanation:

  • slices: Numerical values representing each category
  • labels: Category names shown on chart
  • main: Title of the chart

✅ Pie Chart with Percentages

slices <- c(25, 25, 30, 20)
labels <- c("A", "B", "C", "D")
pct <- round(slices / sum(slices) * 100)
labels_pct <- paste(labels, pct, "%")
pie(slices, labels = labels_pct, main = "Category Share")

🔍 Explanation:

  • round(slices / sum(slices) * 100): Calculates percentage share
  • paste(): Combines label with percentage
  • Shows both label and % on the chart

📊 2. Bar Charts in Base R

✅ Vertical Bar Chart

counts <- table(mtcars$cyl)
barplot(counts, main = "Car Cylinders",
        xlab = "Cylinders", ylab = "Count",
        col = "lightblue", border = "black")

🔍 Explanation:

  • table() counts frequency of cylinder values
  • barplot() displays those counts as vertical bars
  • col, border: Adds color and border styling

✅ Horizontal Bar Chart

barplot(counts, horiz = TRUE,
        main = "Horizontal Bar Chart",
        xlab = "Count", col = "orange")

✅ Stacked Bar Chart Example

data <- matrix(c(3, 2, 5, 4), nrow = 2)
barplot(data, main = "Stacked Bars", col = c("blue", "green"), legend = c("Set A", "Set B"))

📦 3. Bar Charts with ggplot2

✅ Simple Bar Plot

library(ggplot2)
ggplot(mtcars, aes(x = factor(cyl))) +
  geom_bar(fill = "skyblue") +
  labs(title = "Cylinder Distribution", x = "Cylinders", y = "Count")

🔍 Explanation:

  • geom_bar() counts observations for each category
  • factor(cyl) ensures categorical axis
  • fill sets color

✅ Bar Chart with Custom Data

df <- data.frame(
  category = c("A", "B", "C"),
  value = c(10, 20, 15)
)

ggplot(df, aes(x = category, y = value)) +
  geom_col(fill = "steelblue") +
  labs(title = "Category Values")

🔍 Explanation:

  • geom_col() uses actual y-values (vs. geom_bar() which counts)
  • Great for plotting custom category totals

📚 When to Use Bar vs Pie Charts

FeatureBar ChartPie Chart
Shows Counts✅ Yes❌ Not ideal
Shows Proportions✅ Stacked Bars✅ Yes
Precise Comparison✅ High❌ Difficult
Labels✅ Clear❌ May clutter
Best Practice✅ Preferred⚠️ Use sparingly

🔔 Tip: Prefer bar charts for accurate comparison; pie charts are useful for quick, aesthetic proportion views.


🖼️ Save Pie or Bar Charts

png("bar_chart.png", width = 600, height = 400)
barplot(counts, main = "Save Example")
dev.off()

📌 Summary – Recap & Next Steps

Both pie and bar charts are effective tools for categorical data visualization. While bar charts provide clarity and comparison, pie charts are best for quick insights into proportions.

🔍 Key Takeaways:

  • Use pie() for proportions, barplot() for frequency
  • ggplot2 offers layered, customizable charting
  • Prefer bar charts for comparison; pie charts for visual summaries
  • Use geom_bar() (counts) or geom_col() (custom values) in ggplot2

⚙️ Real-World Relevance:
Used in business dashboards, surveys, marketing reports, and presentation graphics where category comparison is vital.


❓ FAQs – Pie and Bar Charts in R

❓ Can I sort bars by height in R?
✅ Yes, reorder the factor:

df$category <- factor(df$category, levels = df$category[order(df$value)])

❓ How do I add labels to each bar in a bar chart?
✅ Use text() in base R:

bp <- barplot(counts)
text(bp, counts, labels = counts, pos = 3)

❓ What’s the difference between geom_bar() and geom_col()?
geom_bar() counts automatically; geom_col() uses given y-values.

❓ How do I make a 3D pie chart?
✅ Use plotrix::pie3D() from the plotrix package.

❓ Can I use hex or RGB colors in plots?
✅ Yes:

barplot(counts, col = "#1f77b4")

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

R – Pie Charts / Bar Charts / Bars

Or Copy Link

CONTENTS
Scroll to Top