1

I am working on a waffle chart and there's one aesthetic that I am having trouble with. Currently I have my waffle chart filled row-wise, but it's starting at the bottom (with category A) and filling in left to right from the bottom up. I would like to have it fill from the top (starting with category C) left to right. For example, with this implementation, at y = 0.5, the three blue squares that are on the right would instead be on the left. I haven't been able to find any arguments that address this issue.

library(waffle)
library(tidyverse)

my_data <- data.frame(category = c('A', 'B', 'C'),
                   count = c(20, 15, 40),
                   stringsAsFactors=TRUE)

my_data %>%
  ggplot(aes(fill = category, values = count)) +
  geom_waffle(
    n_rows = 10,
    size = 0.33,
    colour = "white",
    flip = TRUE,
    make_proportional = TRUE,
    na.rm = T
  )

Resulting waffle chart

I've looked through the documentation for geom_waffle, and multiple existing stackoverflow threads about geom_waffle, but I haven't found any answers from these sources.

1 Answers1

3

Not sure with waffle, but with dplyr, tidyr::uncount, and ggplot2:

library(tidyverse)
my_data |>
  uncount(round(100 * count / sum(count))) |>
  mutate(num = row_number() - 1,
         row = 10 - num %% 10,
         col = num %/% 10) |>
  ggplot(aes(row, col, fill = category)) +
  geom_tile(color = "gray90", linewidth = 1) +
  coord_equal()

(In retrospect I realize my rounding here won't work in all cases, eg. if you have the same number for each of three categories, it will assign 33 tiles to each, for a total of 99. Forcing the rounded percents to add to 100 is a little fiddly. One way could be to calculate the last category as the difference between 100 and the cumulative sum to that point. I think they do something similar here: Rounded percentages that add up to 100% in group_by statement)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53