Search code examples
rggplot2waffle-chart

How to increase geom_waffle plot's size?


I am trying to make a waffle plot using geom_waffle() in R. Here is my code so far;

library(tidyverse)
library(waffle)
library(ggthemes)

haunted_places <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-10-10/haunted_places.csv')
haunted_places_top_5 <- haunted_places %>% 
  mutate(last_word = stringr::str_extract(location, "\\S+$"),
         last_word = str_to_title(last_word),
         last_word = case_when(last_word == "Rd." ~ "Road",
                               last_word == "Rd" ~ "Road",
                               last_word == "Streets" ~ "Street",
                               last_word == "Street)" ~ "Street", 
                               last_word == "St." ~ "Street", 
                               .default = as.character(last_word))) %>%
  group_by(last_word) %>%
  mutate(count = n(),
         percentage = count/10992,
         total_count = 10992) %>%
  select(last_word, count, total_count, percentage) %>%
  distinct() %>%
  arrange(desc(count))

haunted_places_top_5 <- haunted_places_top_5[1:5, ] 

ggplot(haunted_places_top_5, aes(fill = last_word, values = count)) +
  geom_waffle(color = "white", size = 0.25, na.rm = TRUE, nrows = 10) +
  facet_wrap(~last_word, nrow = 2) +
  theme_fivethirtyeight() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        legend.position = "none") +
  coord_fixed(ratio = 1)

And, this code returns the following plot;

enter image description here

My problem with this visualization is mainly due to small size of the tiles. Is there way or a trick that can help to customize the size of facetted panels so that they can be seen better by the audience? Thank you for your kind attention beforehand.


Solution

  • A feasible approach involves scaling down the count values by dividing them by 10. This adjustment effectively reduces the count values, resulting in each tile of the waffle plot representing 10 units, rather than just one. Subsequently, this change can be clearly annotated on the plot for clarity.

    plot <- ggplot(haunted_places_top_5, aes(fill = last_word, values = count/10)) +  # Scale down the count
      geom_waffle(color = "white", size = 0.25, na.rm = TRUE, nrows = 5) +
      facet_wrap(~last_word, nrow = 2) +
      theme_fivethirtyeight() +
      theme(panel.grid.major.x = element_blank(),
            panel.grid.major.y = element_blank(),
            axis.text.x = element_blank(),
            axis.text.y = element_blank(),
            legend.position = "none") +
      coord_fixed(ratio = 1) +
      annotate("text", x = Inf, y = Inf, label = "Each tile represents 10 units", 
               hjust = 1.1, vjust = 1.1, size = 3, color = "black")  # Add annotation
    plot
    

    enter image description here