18  Waffle charts

Pie charts are the devil. They should be an instant F in any data visualization class. The problem? How carefully can you evaluate angles and area? Unless they are blindingly obvious and only a few categories, not well. If you’ve got 25 categories, how can you tell the difference between 7 and 9 percent? You can’t.

So let’s introduce a better way: The Waffle Chart. Some call it a square pie chart. I personally hate that. Waffles it is.

A waffle chart is designed to show you parts of the whole – proportionality. How many yards on offense come from rushing or passing. How many singles, doubles, triples and home runs make up a teams hits. How many shots a basketball team takes are two pointers versus three pointers.

First, install the library in the console. We want a newer version of the waffle library than is in CRAN – where you normally get libraries from – so copy and paste this into your console:

install.packages("waffle")

Now load it:

library(waffle)

18.1 Making waffles with vectors

Let’s look at the debacle that was Nebraska vs. Michigan State in fall 2021 in college football. Here’s the box score, which we’ll use for this part of the walkthrough.

Maybe the easiest way to do waffle charts, at least at first, is to make vectors of your data and plug them in. To make a vector, we use the c or concatenate function.

So let’s look at offense. Rushing vs passing.

nu <- c("Rushing"=187, "Passing"=255)
ms <- c("Rushing"=71, "Passing"=183)

So what does the breakdown of the night look like?

The waffle library can break this down in a way that’s easier on the eyes than a pie chart. We call the library, add the data, specify the number of rows, give it a title and an x value label, and to clean up a quirk of the library, we’ve got to specify colors.

waffle(
        nu, 
        rows = 10, 
        title="Nebraska's offense", 
        xlab="1 square = 1 yard", 
        colors = c("black", "red")
)

Or, we could make this two teams in the same chart.

passing <- c("Nebraska"=255, "Mighigan State"=183)
waffle(
        passing, 
        rows = 10, 
        title="Nebraska vs Michigan State: passing", 
        xlab="1 square = 1 yard", 
        colors = c("red", "black")
)

So what does it look like if we compare the two teams using the two vectors in the same chart? To do that – and I am not making this up – you have to create a waffle iron. Get it? Waffle charts? Iron?

iron(
 waffle(nu, 
        rows = 10, 
        title="Nebraska's offense", 
        xlab="1 square = 1 yard", 
        colors = c("black", "red")
        ),
 waffle(ms, 
        rows = 10, 
        title="Michigan State's offense", 
        xlab="1 square = 1 yard", 
        colors = c("black", "red")
        )
)

What do you notice about this chart? Notice how the squares aren’t the same size? Well, Nebraska out-gained Michigan State by a long way … AND LOST. So the squares aren’t the same size because the numbers aren’t the same. We can fix that by adding an unnamed padding number so the number of yards add up to the same thing. Let’s make the total for everyone be 442, Nebraska’s total yards of offense. So to do that, we need to add a padding of 188 to Michigan State. REMEMBER: Don’t name it or it’ll show up in the legend.

nu <- c("Rushing"=187, "Passing"=255)
ms <- c("Rushing"=71, "Passing"=183, 188)

Now, in our waffle iron, if we don’t give that padding a color, we’ll get an error. So we need to make it white. Which, given our white background, means it will disappear.

iron(
 waffle(nu, 
        rows = 10, 
        title="Nebraska's offense", 
        xlab="1 square = 1 yard", 
        colors = c("black", "red")
        ),
 waffle(ms, 
        rows = 10, 
        title="Michigan State's offense", 
        xlab="1 square = 1 yard", 
        colors = c("dark green", "black", "white")
        )
)

One last thing we can do is change the 1 square = 1 yard bit – which makes the squares really small in this case – by dividing our vector. Remember what you learned in Swirl about math on vectors?

iron(
 waffle(nu/2, 
        rows = 10, 
        title="Nebraska's offense", 
        xlab="1 square = 2 yards", 
        colors = c("black", "red")
        ),
 waffle(ms/2, 
        rows = 10, 
        title="Michigan State's offense", 
        xlab="1 square = 2 yards", 
        colors = c("dark green", "black", "white")
        )
)

News flash: Nebraska beat Michigan State everywhere but on the scoreboard and Nebraska is changing its fight song to “Everybody Hurts” by REM.