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



Contact

This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message on Twitter, or send an email pasting yan.holtz.data with gmail.com.

Github Twitter