2.6 Pie and Waffle Charts
Objective
Sometimes, you need to be able to display data that clearly shows how parts of your data relate to all of your data. A pie chart is a circular plot divided into sectors, or slices. In this chapter, you will learn how to create a pie chart in R from a collection of numbers that represent frequencies (or counts) of events or outcomes that fall into different categories. Unfortunately, it is really easy to create bad pie charts, so you'll also learn how to create a waffle chart as an alternative.
About Pie Charts
Again: It is really easy to create bad pie charts. Use at your own risk.
Each slice of the pie should be proportional to the occurrence of observations.
The observations in all of the slices must add up to 100%. If the percentages in all your slices add up to less than 100% or greater than 100%, you have a very bad (and misleading) pie chart. Pie charts are meant to display proportions of a whole, and nothing else.
Good pie charts adhere to the Area Principle. This means that each slice of the pie has an area that corresponds to the percentage of the total number of observations that make up that slice. If 25% of your observations fall into one category, that category should make up exactly a quarter of the pie. No more, no less.
Good pie charts are labeled nicely. I like when pie charts show very clearly both the counts of an item in a particular category, as well as the percentage of the total number of observations in that category.
Membership into each category should be mutually exclusive. That is, you don’t want an observation to appear in multiple slices.
You will always have to consider whether a pie chart or bar chart is a better way to display your data. If you’re trying to illustrate a collection of items that naturally add up to 100%, a pie chart may be appropriate. If there are multiple categories and it may be difficult to distinguish which slice of your pie is bigger (e.g. one observation is 28% and another is 29%) a bar chart may be more appropriate. When small variations between categories should be communicated to your audience, bar charts are typically more effective than pie charts in presenting your data.
I’ve seen a lot of reports that use snazzy looking 3D pie charts (usually created in Excel), but usually, creating a 3D pie chart is a very bad idea. That’s because a 3D pie chart will rarely, if ever, conform to the area principle.
If You Counted Your Observations Already
To create a pie chart with ggplot, you will need a dataframe with counts of something in different categories. Here is an example of data generated by opening one package of regular M&Ms to investigate the distribution of colors:
Next, create a new.df to store variables for the proportion of the pie each slice should take, and build a label for each slice. Using dplyr from tidyverse, you can do it all at once:
color count prop label
1 blue 12 24.5 BLUE 24.5%
2 brown 6 12.2 BROWN 12.2%
3 green 8 16.3 GREEN 16.3%
4 orange 10 20.4 ORANGE 20.4%
5 red 6 12.2 RED 12.2%
6 yellow 7 14.3 YELLOW 14.3%
Now it’s time to generate the pie chart in base R, which is simple and straightforward. Be sure to install gplots and load it with library(gplots) first, which makes sure your colors are rendered properly in all versions of R:
With ggplot, pie charts are single-bar bar charts that you curl around into a pie shape using polar coordinates. Yes, it’s pretty complicated – and labels are a particular pain in the you know what. That’s why (if I absolutely must create a pie chart, which I try to avoid) I stick with base R for the pies.
Let’s break down all the elements of that long command. On the first line, we send the data frame new to the ggplot plotter (new %>% ggplot). As we launch ggplot, we want to tell it to plot the data from our count column as the slices, and fill those slices based on unique data in the color column. On the second line, we request ar bar plot using geom_bar. The stat="identity" argument tells ggplot to use the counts we provided, rather than trying to count up observations itself. Next, use coord_polar to wrap the bar into a pie shape (theta will always equal y for a pie chart). On the third line, use geom_text to place the labels on the pie. The text for the labels will come from the label column in our data frame (label=label), the font size will be 3, and the labels will be 0.6 units from the center of the pie (try changing to vjust=2 to see what happens). (In ggplot we use label, but in base R it’s labels.) Finally, we color the slices using scale_fill_manual and the colors in the color column, changed to the hex codes that ggplot will use to get accurate colors using the col2hex function from the gplots package. Finally, add a ggtitle and clean up the plot with theme_void().
If You Need R to Count Observations for You
Alternatively, you may have your data in case format (meaning you haven't added up all the data, but you still want to create a pie chart). Let’s use my CSV (comma-separated values) file containing 1922 rows, one row per M&M. gathered by an entire class of students. It has been published to the web from Google Spreadsheets and assigned a shortlink. (I split the import statement into two lines just so it’s easier to read here… you could also replace dataurl in the second line with the URL in quotes, and it would work fine.)
The data looks like this:
# A tibble: 1,922 x 7
student id color defect full.bag.weight empty.bag.weight total.number
<chr> <int> <chr> <chr> <dbl> <dbl> <int>
1 alborb 1 R N 49.4 1.02 55
2 alborb 2 BR L 49.4 1.02 55
3 alborb 3 G N 49.4 1.02 55
Now that you have the M&Ms data loaded into the mnms variable, you can ask R to tally up all of your color counts. (Replace color with defect to get defect counts).
# A tibble: 6 x 2
color n
<chr> <int>
1 BL 311
2 BR 240
3 G 375
4 O 420
5 R 319
6 Y 257
To generate the pie chart, we need to generate some labels just like we did last time. Notice that we also need to convert the color codes into color names so that they render correctly:
# A tibble: 6 x 4
color n prop label
<chr> <int> <dbl> <chr>
1 blue 311 0.162 blue: 16.18%
2 brown 240 0.125 brown: 12.49%
3 green 375 0.195 green: 19.51%
4 orange 420 0.219 orange: 21.85%
5 red 319 0.166 red: 16.6%
6 yellow 257 0.134 yellow: 13.37%
Now we have a data frame called, just like last time, new. We can plot in base R:
Or alternatively, using ggplot:
This code is identical to the code on the bottom of page 122, except we’ve replaced the word count in the first line of code with the variable name n – that’s the name the tally command gave our counts when we were creating the new data frame.
The bottom line on pie charts is this: they can be useful to visualize the distribution of proportions of an observation between categories, but ONLY if 1) the slices adhere to the Area Principle, 2) the slices add up to exactly 100%, and 3) there are no slight variations between slices that are not easily distinguished on the pie chart. It is very easy to create bad, misleading pie charts. They should be used with care and caution.
Waffle Charts
Waffle charts are an alternative to pie charts that still enable you to communicate how parts of a data set are related to the whole. First, install this ggplot-based package and wake it up using the library command:
The waffle package consists of one command: waffle. We will create a waffle chart from the same M&M data we just used to create the pie charts. Since there are so many M&Ms, let’s make each square on the waffle equal 10 M&Ms, and set the waffle chart to 10 rows: