Visualizing Data Delight: A Guide to Waffle Charts in R

 

Introduction:

Waffle charts are a delightful way to present data, turning numbers into visually appealing, bite-sized morsels. Also known as square pie charts, they are a type of data visualization that represents parts of a whole using a grid of squares. Each square represents a certain proportion of the total value, and the squares are filled in to reflect the corresponding proportions. Waffle charts are a great alternative to pie charts when there are too many categories to display effectively. Whether you’re a seasoned data scientist or just starting with R, these charts add a dash of creativity to your visualizations.

 

In this blog post, we will learn how to create waffle charts in R using waffle package and geom_waffle() from ggplot2 package. We will also cover some of the customization options that are available.

 

The data that we will be using today has the sales of 6 different fruits/vegetables in a week. Let’s dive in and transform your data into a visually enticing treat!

    Commodity       Date Average Unit
1 Pomegranate 11-09-2013   210.0   Kg
2       Lemon 11-09-2013    32.5   Kg
3      Ginger 11-09-2013   145.0   Kg

 

1. Using waffle package

To create a basic waffle chart using waffle, we will first need to install and load said package. We can do this using the following commands:

install.packages("waffle", repos = "https://cinc.rud.is")
library(waffle)

 

Using dplyr, we will summarise the data according to the commodities and then create the waffle chart.
library(dplyr)

df1 <- df %>% group_by(Commodity) %>% summarise(Count = n())
df1 %>% as.data.frame()
        Commodity Count
1 Coriander Green     8
2          Ginger     8
3           Lemon     3
4            Mint     6
5     Pomegranate     8
6    Spinach Leaf     7
waffle(df1, rows = 5, legend_pos = "bottom")

We can specify the number of rows we want in the plot. THe position of the legend can also be changed as per our choice - possible values are “right”, “left”, “top”, “bottom”, and “none”.

 

We can change the colours of the plot in the following way:

colors <- c("#210578", "#b65ea8", "#dfb9c2", "#5a1d8d", "#ca89ac", "#923aa1")

waffle(df1, rows = 5, colors = colors, legend_pos = "bottom")

 

2. Using geom_waffle() from ggplot2

Here we will have to install and load ggplot2 in order to use the geom_waffle() function.

install.packages("ggplot2")
library(ggplot2)

 

We will be using the previously summarised data in this example as well.
ggplot(df1, aes(fill = Commodity, values = Count)) +
  geom_waffle(n_rows = 5, size = 0.5, colour = "white", na.rm = T)

“n_rows” is used to specify the number of rows in the plot, “size” to specify the space between each tile and “colour” to change the colour of the aforementioned space.

 

We can change the colours of the plot and the legend position here as well, with the help of “scale_fill_manual()”. To make this plot designed by using ggplot2 look more like the waffle one, we can use “coord_equal()” to make sure that the units are equally scaled on both the axes (i.e. to acquire square tiles) and “theme_void()” to remove the gridlines and labels.

ggplot(df1, aes(fill = Commodity, values = Count)) +
  geom_waffle(n_rows = 5, size = 2, colour = "white", na.rm = T) +
  scale_fill_manual(name = NULL,
                    values = c("#210578", "#b65ea8", "#dfb9c2", "#5a1d8d", "#ca89ac", "#923aa1"),
                    labels = c(df1$Commodity)) +
  coord_equal() +
  theme_void() +
  theme(legend.position = "bottom")

 

Interpreting the waffle charts:

Each square in the waffle chart represents a portion of your data.

sum(df1$Count)
[1] 40

In our example, we have 40 observations in total; hence, each square could represent 2.5% of the total commodities sold.

 

Conclusion:

Waffle charts are a versatile and informative way to visualize data in R. geom_waffle() works in a very similar way to that of waffle(), but to get to that point, we have to specify more arguments, i.e. it gets a little verbrose. Even with that, both the methods that we covered in this blog post are very easy to understand and work with.

Through this guide, you’ve learned the basics of creating waffle charts and customizing them to tell your data’s story. Experiment with your own datasets and discover the artistic side of data visualization in R. Happy waffling!