Open In App

Creating a Waffle Plot Together with Facets in ggplot2 in R

Last Updated : 09 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Visualizing categorical data in a compact and easy-to-understand format is essential in data analysis. Waffle plots are a powerful way to represent parts of a whole, where each block or "waffle" corresponds to a fixed number of units. With ggplot2 in R, you can not only create beautiful waffle plots but also use facets to compare different categories or subgroups simultaneously. In this article, we’ll discuss how to create waffle plots with facets in ggplot2 and waffle packages using R Programming Language.

Overview of Waffle Plots

A waffle plot is a type of chart that represents categorical data using a grid of squares (waffles), with each square representing a proportion of the total. This chart is often used as an alternative to pie charts because it can be more effective at visualizing proportions, particularly when working with small datasets.

Each square in a waffle plot corresponds to a fixed number of observations. Waffle plots are ideal for:

  • Visualizing parts-to-whole relationships (like percentages).
  • Offering a more intuitive understanding of proportions in comparison to pie charts.

In ggplot2, waffle plots are not built in by default, but they can be easily created using external libraries such as waffle and cowplot.

Using Facets with Waffle Plots

Faceting in ggplot2 allows you to create multiple small versions of a plot, one for each level of a categorical variable. When combined with waffle plots, faceting is useful for comparing categorical data across different groups or subcategories. For example, if you have survey data showing the proportions of different product preferences across several age groups, you can use faceting to create a separate waffle plot for each age group. This makes it easier to compare the proportions across groups visually.

To follow along with this tutorial, ensure you have the following R packages installed:

  • ggplot2: For creating the plots.
  • waffle: For generating the waffle plot.
  • dplyr: For data manipulation.
library(ggplot2)
library(waffle)
library(dplyr)

1: Creating a Basic Waffle Plot

Let's start by creating a simple waffle plot using the waffle package. We will use a sample dataset representing different fruit sales in a store:

R
# Sample data representing different fruit sales
fruit_sales <- c(Apple = 40, Banana = 30, Orange = 20, Grape = 10)
# Create the basic waffle plot
waffle_plot <- waffle(fruit_sales, rows = 5, size = 0.5, 
                      title = "Fruit Sales Waffle Plot", 
                      colors = c("#FF9999", "#FFCC99", "#99CCFF", "#CCCCCC"))
print(waffle_plot)

Output:

gh
Creating a Basic Waffle Plot
  • rows = 5: This parameter specifies the number of rows in the waffle plot grid.
  • colors: Sets the color scheme for the different categories.

2: Creating a Waffle Plot Using ggplot2

We can also create a waffle plot using ggplot2. This method provides more flexibility and control over the visualization.

R
# Sample data representing different fruit sales
fruit_data <- data.frame(
  Category = rep(c("Apple", "Banana", "Orange", "Grape"), times = c(40, 30, 20, 10))
)

# Create a column for the waffle grid position
fruit_data$pos <- seq(1, nrow(fruit_data))
fruit_data$y <- ceiling(fruit_data$pos / 10)
fruit_data$x <- fruit_data$pos %% 10
fruit_data$x[fruit_data$x == 0] <- 10
# Create the ggplot2 waffle plot
ggplot(fruit_data, aes(x = x, y = y, fill = Category)) +
  geom_tile(color = "white", size = 0.8) +
  scale_fill_manual(values = c("Apple" = "#FF9999", "Banana" = "#FFCC99", "Orange" = "#99CCFF", 
                                                                        "Grape" = "#CCCCCC")) +
  labs(title = "Fruit Sales Waffle Plot (ggplot2)", x = NULL, y = NULL) +
  theme_minimal() +
  theme(
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold")
  ) +
  coord_fixed(ratio = 1)

Output:

gh
Creating a Waffle Plot Using ggplot2

This code creates a waffle plot using geom_tile() in ggplot2.

Customizing the Waffle Plot

You can further customize your waffle plot with various options:

  • Change the number of rows using the rows parameter.
  • Modify colors using the scale_fill_manual() function.
  • Adjust the spacing between facets using facet_grid().

Conclusion

Waffle plots are a visually appealing way to represent categorical data distributions. Using the ggplot2 and waffle packages, you can create basic waffle plots or more advanced plots with faceting for multiple groups. By adding facets, you can showcase multiple categories or groups, making your visualization more comprehensive and insightful.


Next Article

Similar Reads