## Waffle Charts

I guess all of you are familiar with visualizations to depict a whole, or part of the whole distribution like pie charts. Today we learn how to create a waffle chart which does essentially the same, but gives your audience a visual intuition in terms of percentage. So, let’s waffle.

Waffle charts are easily made in R, even if you are not very familiar how R works. First, we need to install the waffle package in case we never used that package before. You can install the package with the code:install.packages("waffle") and load the package after the installation is done: library(waffle). R uses ggplot in the background, so we need to load the package as well.

#install.packages("waffle") to install the package
library(waffle)
library(ggplot2)

## Basic waffle charts are pretty easy.

All we need to do is give R a vector with values, for illustration purposes let’s say c(50, 30, 15, 5) and determine how many rows you want to depict in the waffle. Here we choose 10 row which would then simply add up to a percentage interpretation. Let’s see how this works.

waffle(c(50, 30, 15, 5), rows = 10, title = "Your Title")

So, the basic command just display the vectors based on the waffle grid. Nothing more, nothing less. However, you can adjust that basic plot in many ways by using the GGPlot syntax.

## The Power of GGPlot

Let’s make use of our knowledge about GGPlot and its many attributes and commands to produce a chart. Again, we give R a vector with values parts <- c() but this time we give also labels for the groups to display. In addition to the basic waffle syntax, we append a vector with colour values, you can use labels (red) for the colours as well. Furthermore, we label the x-axis and store the graph as waffle1.

Let’s see how it works.

parts <- c(Left-wing=(25), Uncertain=30, Right-wing=45)

waffle1<- waffle(parts, rows=10, size=1, colors=c("#969696", "#1879bf", "#009bda"),
title="Another Waffle",
xlab="")

waffle1

Now let’s further adjust our graph and provide some info’s for our audience. Now we can see why GGplot works behind the waffle chart. As in the GGplot universe we can simply add (+) attributes.

waffle1 +
ylab("") +
xlab("Squares indicate percentages.") +
ggtitle("Fake voting intention")

Hence, once you set up this chart you can simply reproduce it just by providing R the indicators, we you want to display.

## Pictograms

R come with a nice feature provided by fontawesom. We can include pictograms. All we need to do is import the font type and install on your own machine. So, try it at home.

Go to: http://fontawesome.io/ and download the desktop package. Then install the font by clicking the corresponding ttf file: fontawesome-webfont.ttf and import it in R by importing all fonts from your working machine. Check if everything went well by fonts()[grep("Awesome", fonts())] before we can start to use pictograms.

# We need first more Fonts for R. Install first in case your using it the first time.
library(extrafont)
#Import all fonts from your working machine
font_import()

# check that Font Awesome is really imported
fonts()[grep("Awesome", fonts())]
# You should get this respond: [1] "FontAwesome"

# use this command if things look odd in RStudio under Windows
loadfonts(device = "win")

All seem to worked out well? Okay, then let’s use pictograms with the use_glyph option. Just provide the name of the pictogram, in the background R downloads and uses font awesome to depict it in your waffle. Let’s try to depict children by use_glyph = "child".

#Let's depict kids!
waffle(c(50, 30, 15, 5), rows = 10, use_glyph = "child", glyph_size = 6,
title = "Some meaningful Title")

This is a nice feature because we can use pictograms in several instances. For example, in data reports and other documents we can simply create in R. So, let’s waffle.

#Let's depict more!
waffle(c(25, 30, 45), rows = 10, use_glyph = "university", glyph_size = 6,
colors=c("#969696", "#1879bf", "#009bda"),
xlab="",
title = "")