Skip to contents

A waffle chart is a grid of squares (by default 10 x 10 = 100 cells) in which each category fills a number of cells proportional to its share of the whole. Each cell therefore reads as "one percent" (or one unit of the total), which makes part-to-whole comparisons far easier to read than a pie chart. The format is a staple of Pew Research Center, FiveThirtyEight, The Economist and the Washington Post, and was popularised in R by Bob Rudis's waffle package.

Cells are allocated with the largest-remainder method, so the cell counts always add up exactly to rows * cols. Unlike parliament_chart, which draws individual seats along a hemicycle, a waffle chart uses a rectangular grid to show proportions of a whole rather than absolute counts.

Usage

waffle_chart(
  data,
  category,
  value,
  rows = 10,
  cols = 10,
  total = NULL,
  colors = NULL,
  emptyCol = "#e5e5e5",
  cellPadding = 2,
  cellRadius = 2,
  direction = "row",
  legend = TRUE,
  legendFontSize = 12,
  hover = TRUE,
  animate = TRUE,
  animationDuration = 1200,
  title = NULL,
  titleFontSize = 16,
  font = "Verdana, Geneva, Tahoma, sans-serif",
  bgcol = "white",
  textCol = "black",
  width = NULL,
  height = NULL
)

Arguments

data

The data frame containing the variables to consider. It should contain one row per category.

category

The name of the categorical column used as labels.

value

The name of the numeric (non-negative) column holding each category's value.

rows

Number of rows in the grid. Defaults to 10.

cols

Number of columns in the grid. Defaults to 10. The total number of cells is rows * cols.

total

Optional. The value represented by the whole grid. Defaults to the sum of value. When larger than that sum, the remaining cells are left unfilled and drawn with emptyCol.

colors

Optional. A character vector of colors, one per category (recycled if shorter). Defaults to a Tableau-10 style palette.

emptyCol

Color of the unfilled cells. Defaults to "#e5e5e5".

cellPadding

Gap between cells in pixels. Defaults to 2.

cellRadius

Corner radius of each cell in pixels. Defaults to 2. Use 0 for sharp squares.

direction

Fill direction. "row" (default) fills row by row starting from the bottom-left corner (the Pew Research style), "col" fills column by column starting from the bottom-left corner.

legend

Whether to display a legend showing each category and its percentage. Defaults to TRUE.

legendFontSize

Font size of the legend text. Defaults to 12.

hover

Whether hovering a cell or a legend entry highlights that category and shows a label with its value and percentage. Defaults to TRUE.

animate

Whether the cells pop in sequentially when the chart loads. Defaults to TRUE.

animationDuration

Total duration of the load animation in milliseconds. Defaults to 1200.

title

Optional. Title of the chart.

titleFontSize

Font size of the chart title. Defaults to 16.

font

The font family used for all text. Defaults to "Verdana, Geneva, Tahoma, sans-serif".

bgcol

Background color of the SVG. Defaults to "white".

textCol

Color of the title and legend text. Defaults to "black".

width

Optional. The width of the SVG output.

height

Optional. The height of the SVG output.

Value

An SVG waffle chart.

Examples

# How respondents answered a survey question
survey <- data.frame(
  response = c("Strongly agree", "Agree", "Neutral",
               "Disagree", "Strongly disagree"),
  n = c(312, 405, 158, 87, 38)
)

waffle_chart(
  data     = survey,
  category = "response",
  value    = "n",
  colors   = c("#1a9641", "#a6d96a", "#bdbdbd", "#fdae61", "#d7191c"),
  title    = "Do you support the new policy?"
)
# Share of the world population by continent in 2007, filled by column library(gapminder) pop_2007 <- gapminder[gapminder$year == 2007, ] pop_continent <- aggregate(pop ~ continent, data = pop_2007, FUN = sum) waffle_chart( data = pop_continent, category = "continent", value = "pop", direction = "col", rows = 10, cols = 20, title = "World population by continent, 2007" )