12  Waffle charts

Graphs in this chapter use ggrewindr::theme_pogs(), modeled on Pogs: a cardboard milk cap background, a navy frame and title, red, sky blue, yellow, teal, purple, and rust prints, and a Caveat title.


Requirements:

✅ a categorical variable

12.1 Description

Waffle charts split a whole into parts with small squares (or icons) that fill a grid, one color per category. They’re useful for showing percentages or proportions of a total, and a legend or labels can explain each category. Put the legend on the top or bottom and lay it out horizontally, so the grid keeps its shape and stays easy to read.

We’ll build a waffle chart using the ggwaffle package.

12.2 Set up

PACKAGES

Install packages.

show/hide
# ggwaffle is GitHub-only
install.packages("pak")
pak::pak("liamgilbey/ggwaffle")
library(ggwaffle)
library(ggplot2)
library(dplyr) # for tibble() and data manipulation

DATA

In the mid 90s, kids traded and battled with Pogs: cardboard milk caps, printed and holographic pogs, and heavy plastic “slammers” used to flip them. We’ll simulate a collection of 100 pieces with sample(), giving each type its own chance of turning up.

Before we can draw a waffle, the data needs a special transformation with ggwaffle::waffle_iron(). Set the group argument in ggwaffle::aes_d() to the categorical variable you want to see the relative counts for, and set rows = 10 so 100 pieces fill a 10 by 10 grid. Setting the seed with set.seed() means you’ll get the same collection every time you run the code below.

show/hide
set.seed(1993) # same 100 pogs every time the code runs
pog_collection <- tibble(
  pog_id = 1:100, # 100 different pogs, one row each
  type = sample(
    c("Milk caps", "Printed pogs", "Holographic", "Slammers"), # four kinds of pogs
    size = 100, replace = TRUE, # one kind for each pog
    prob = c(0.45, 0.30, 0.15, 0.10) # milk caps are the most common
    )
  )
waffle_pogs <- ggwaffle::waffle_iron(pog_collection,
                      aes_d(group = type), # one square per pog, grouped by kind
                      rows = 10) # a 10 by 10 grid

glimpse(waffle_pogs)
#> Rows: 100
#> Columns: 3
#> $ y     <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, …
#> $ x     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2…
#> $ group <chr> "Holographic", "Holographic", "Hol…

12.3 Distribution

A waffle chart is built for one special total: 100. When there are 100 squares, every square is one percent, so you can read a percentage just by counting squares. Below we’ll move from the collection, to a picture, to the formula.

COUNT IT

Start with the first ten pieces in the collection.

show/hide
head(pog_collection$type, 10)
#>  [1] "Milk caps"    "Milk caps"    "Milk caps"   
#>  [4] "Printed pogs" "Printed pogs" "Printed pogs"
#>  [7] "Milk caps"    "Milk caps"    "Holographic" 
#> [10] "Milk caps"

Six of these ten are milk caps, three are printed pogs, and one is holographic. Counting all 100 pieces gives us counts that are also percentages. The expected column is what we’d get if the collection matched the probabilities in sample() exactly.

show/hide
pog_collection |> 
  count(type) |> 
  mutate(expected = c(15, 45, 30, 10)) 

DRAW IT

A bar model shows the collection as one bar of 100 pieces split into four parts.

show/hide
pog_collection |> 
  count(type) |> 
  ggplot(aes(x = n, y = "100 pieces", fill = type)) + 
  geom_col() + 
  geom_text(aes(label = n), color = c("white", "white", "white", "black"), 
    position = position_stack(vjust = 0.5)) + 
  labs(x = "Number of pieces", y = NULL, fill = "Type")

A waffle chart is the same bar folded into columns of 10. Each column of the waffle below is ten pieces. The milk caps fill a little more than four columns, and the slammers fill a little more than one.

show/hide
ggplot(data = waffle_pogs, aes(x = x, y = y, fill = group)) + 
  ggwaffle::geom_waffle() + 
  coord_equal() + 
  labs(x = NULL, y = NULL, fill = NULL) + 
  theme(axis.text = element_blank(),
        panel.grid = element_blank(),
        legend.position = "top") +
  guides(fill = guide_legend(nrow = 2))

NAME IT

Every piece in the collection is an independent draw from four types, so the four counts follow a multinomial distribution:

\[ (N_{\text{milk}}, N_{\text{printed}}, N_{\text{holo}}, N_{\text{slam}}) \sim \text{Multinomial}(n = 100, \; p = (0.45, 0.30, 0.15, 0.10)) \]

With \(n = 100\), the expected count for each type is its probability times 100, which is the same number as its percentage. The counts wobble around those expectations by about \(\sqrt{100\,p\,(1 - p)}\) pieces: about 5 for milk caps and 3 for slammers. Our collection has 14 slammers instead of the expected 10, a difference of a little more than one standard deviation. Change the number in set.seed() and the squares trade places, but they always add to 100.

CHANGE IT

The waffle above is what we expect: 100 pieces, so every square is one percent. Real collections rarely land on 100. Below we’ll keep the same four types and the same chances but shrink the collection to 47 pieces (a kid who just started trading at recess). We should still see the milk caps fill about half the grid, but the squares will stop being percentages.

show/hide
set.seed(1993) # same seed as above
pog_starter <- tibble(
  pog_id = 1:47, # a smaller collection
  type = sample(
    c("Milk caps", "Printed pogs", "Holographic", "Slammers"),
    size = 47, replace = TRUE,
    prob = c(0.45, 0.30, 0.15, 0.10) # same chances as above
    )
  )
waffle_starter <- ggwaffle::waffle_iron(pog_starter,
                      aes_d(group = type), rows = 10)
pog_starter |>
  count(type) |>
  mutate(percent = round(100 * n / sum(n), 1))

We’ll redraw the same waffle with the smaller collection.

show/hide
ggplot(data = waffle_starter, aes(x = x, y = y, fill = group)) +
  ggwaffle::geom_waffle() +
  coord_equal() +
  labs(x = NULL, y = NULL, fill = NULL) +
  theme(axis.text = element_blank(),
        panel.grid = element_blank(),
        legend.position = "top") +
  guides(fill = guide_legend(nrow = 2))

The grid is now 5 columns wide, and the last column holds only 7 squares, so the waffle has a bite taken out of it. Counting squares no longer gives a percentage. The 25 milk cap squares are 53 percent of the collection, and each square is worth about 2.1 percent. The 3 holographic squares look like a rounding error, but they’re 6 percent.

The shape of the collection survived (milk caps still fill about half the grid), but the reader can’t count their way to the numbers anymore. I’ve found it’s worth checking the total before drawing a waffle. If it isn’t 100, say what one square is worth in the subtitle, or report the percentages next to the graph.

12.4 Grammar

CODE

  • Create labels with labs()

  • Initialize the graph with ggplot() and provide data

  • Map the x and y to the x and y axes

  • Map group to fill

  • Add ggwaffle::geom_waffle()

  • Keep the squares square with coord_equal()

  • Remove the axis text and grid lines, and move the legend to the top with theme()

  • Split the legend into two rows with guides() so it fits above the square waffle

show/hide
labs_waffle <- labs(
  title = "A Pogs collection",
  caption = "Simulated data",
  subtitle = "100 pieces, one square each",
  x = "", y = "", fill = NULL)
ggp2_waffle <- ggplot(data = waffle_pogs, 
       aes(x = x, 
           y = y, 
           fill = group)) + 
  ggwaffle::geom_waffle() +
  coord_equal()
ggp2_waffle + 
  labs_waffle +
  theme(axis.text = element_blank(),
        panel.grid = element_blank(),
        legend.position = "top") +
  guides(fill = guide_legend(nrow = 2))

GRAPH