Waffle chart with waffle and ggplot2



This post explains how to build a waffle chart with R using the geom_waffle() function. It uses the waffle package, provides reproducible code and explain how to customize the chart.

Waffle section Data to Viz

Libraries and dataset


Install the package with install.packages("waffle").

The input dataset is simple: we just have 3 groups, and each one has a value.

# library
#install.packages("waffle")
library(waffle)
 
# Create data
group <- c("group-1","group-2","group-3")
value <- c(13,5,22)
data <- data.frame(group,value)

Waffle with ggplot2


The waffle package provides a geom_waffle() function that allows to build waffle charts with ggplot2.

ggplot(data, aes(fill=group, values=value)) +
  geom_waffle() +
  theme_void()

Colors


You can customize the colors of the waffle chart using the scale_fill_manual() function and the edge color with the color argument:

ggplot(data, aes(fill=group, values=value)) +
  geom_waffle(color = "white") +
  scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
  theme_void()

Labels


You can change labels in legend to the waffle chart:

ggplot(data, aes(fill=group, values=value)) +
  geom_waffle() +
  scale_fill_manual(
    values = c("#999999", "#E69F00", "#56B4E9"),
    labels = c("First group", "Second group", "Third group")) +
  theme_void()

Going further


You might be interested in how to create waffle chart without ggplot and how to create waffle charts with groups and subgroups.

Related chart types


Grouped and Stacked barplot
Treemap
Doughnut
Pie chart
Dendrogram
Circular packing



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥