R – Waffle Chart

Waffle Chart in detail

A waffle chart provides a clear visual representation of how individual components contribute to a whole. It’s particularly useful for tracking progress toward a goal or for showing parts-to-whole relationships. Unlike pie charts, waffle charts use a grid of equal-sized squares to represent data without distorting the proportions.

Implementation in R

We’ll use ggplot2 for its versatile and elegant plotting capabilities along with the waffle package, an extension that simplifies the creation of waffle charts.

Installing Required Packages

To install the necessary packages in R Studio, run:

install.packages("ggplot2")
install.packages("waffle")

Loading the Libraries

Load the libraries with:

library(ggplot2)
library(waffle)

Example: Company Expense Breakdown

Suppose a company has a total expenditure of $100,000, divided into the following categories:

  • Salaries: $40,000
  • Marketing: $20,000
  • Operations: $15,000
  • Research & Development: $10,000
  • Miscellaneous: $15,000

We can represent this data as a vector in R:

Since we want each square to represent $1,000, dividing each value by 1,000 will give us exactly 100 squares (40 + 20 + 15 + 10 + 15).

Plotting the Waffle Chart

Use the following code to create the waffle chart:

waffle(expenses/1000, rows = 5, size = 0.6,
       colors = c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd"),
       title = "Company Expense Breakdown",
       xlab = "1 square = $1,000")

Output:

Comments

Leave a Reply

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