12 Waffle charts
12.1 Description
Waffle charts use small squares or icons to show how a whole thing is divided into parts, grouped into categories that fill a grid. They’re useful for visualizing percentages or proportions from a total and can include a legend or labels to explain each category. Waffle chart legends should be positioned on top or bottom and justified horizontally to preserve shape and improve readability.
We’ll build a waffle chart using the ggwaffle
package.
12.2 Set up
PACKAGES:
Install packages.
show/hide
library(pak)
library(ggwaffle)
install.packages("palmerpenguins")
library(palmerpenguins)
library(ggplot2)
DATA:
Waffle charts require a special data transformation with ggwaffle::waffle_iron()
Set the group argument in ggwaffle::aes_d()
as the categorical variable you want to see the relative counts for:
show/hide
<- palmerpenguins::penguins
penguins <- dplyr::mutate(penguins,
penguins species = as.character(species))
<- ggwaffle::waffle_iron(penguins,
waffle_peng aes_d(group = species))
glimpse(waffle_peng)
#> Rows: 344
#> Columns: 3
#> $ y <int> 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4…
#> $ x <int> 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2…
#> $ group <chr> "Adelie", "Adelie", "Adelie", "Ade…
12.3 Grammar
CODE:
Create labels with
labs()
Initialize the graph with
ggplot()
and providedata
Map the
x
andy
to thex
andy
axesMap
group
tofill
Add
ggwaffle::geom_waffle()
Add
ggwaffle::theme_waffle()
Move the legend to the top with
theme(legend.position = "top")
show/hide
<- labs(
labs_waffle title = "Palmer penguin species",
x = "", y = "", fill = "Species")
<- ggplot(data = waffle_peng,
ggp2_waffle aes(x = x,
y = y,
fill = group)) +
::geom_waffle() +
ggwaffletheme(legend.position = "top")
+
ggp2_waffle +
labs_waffle ::theme_waffle() ggwaffle
GRAPH: