Infographic-style charts using the R waffle package

Infographics. I’ve seen good examples. I’ve seen more bad examples. In general, I prefer a good chart to an infographic. That said, there’s a “genre” of infographic that I do think is useful, which I’ll call “if X were 100 Y”. A good example: if the world were 100 people.

That method of showing proportions has been called a waffle chart and for extra “infographic-i-ness”, the squares can be replaced by icons. You want to do this using R? Of course you do. Here’s how.

There’s not much more here than you’ll find at the Github home of the R packages, waffle and extrafont. I’ve just made it a little more step-by-step.

1. Install the R packages
You need waffle to create waffle charts and extrafont to use icons in the charts.

install.packages(c("waffle", "extrafont"))

library(waffle)
library(extrafont)

2. Install Font Awesome
The icons that waffle can use are supplied by Font Awesome.

Download the zip file by clicking the Download link at their website. You may want to check out the paid option later but for now, just select the free download.

Unzip the file and navigate to the fonts directory. To install the font, use the file named fontawesome-webfont.ttf. For Windows or Mac OS X users, installation is as simple as double-clicking the file and choosing “Install”. I haven’t done this under Linux for a while, it may even be the same procedure these days.

3. Import and register fonts
In R, fonts have to be imported to the extrafont database. You need to do this whenever a new font is installed that you want to use in R.

font_import()

# check that Font Awesome is imported
fonts()[grep("Awesome", fonts())]
# [1] "FontAwesome"

If you run font_import() in a session, you need to register the fonts with the R output device. Otherwise, this occurs when the package is loaded. If you’re using the Windows OS and want to plot to the screen in RStudio, an additional argument is required:

# this should be fine for Mac OSX
loadfonts()
# use this if things look odd in RStudio under Windows
loadfonts(device = "win")

4. Create charts
Now we are ready to waffle. First, your basic waffle chart. The default colours are Colour Brewer “Set2”.

waffle(c(50, 30, 15, 5), rows = 5, title = "Your basic waffle chart")

Next, using icons. You can browse or search for available icon names on this page. You may need to fiddle with the size.

waffle(c(50, 30, 15, 5), rows = 5, use_glyph = "child", glyph_size = 6, 
title = "Look I made an infographic using R!")

You can use the iron() function to append waffle charts, which might be useful in comparisons. Here’s some made-up data involving some aspect of cars in two unnamed countries:

iron(
  waffle(c(no = 80, yes = 20), rows = 5, use_glyph = "car", glyph_size = 6, 
         colors = c("#c7d4b6", "#a3aabd"), title = "Country A"),
  waffle(c(no = 70, yes = 30), rows = 5, use_glyph = "car", glyph_size = 6,
         colors = c("#c7d4b6", "#a3aabd"), title = "Country B")
)

Conclusion
That’s it, more or less. Are the icons any more effective than the squares? In certain settings, perhaps. You be the judge.

4 thoughts on “Infographic-style charts using the R waffle package

  1. I really enjoyed it! I’m learning R and tutorials like this are a really pleasant way to explore the language. Thank you for posting.

    About Font Awesome and Linux: double clicking the file didn’t work for me on Ubuntu. The installation was successful with these commands:
    sudo apt-get update
    sudo apt-get install fonts-font-awesome

  2. Pingback: Using Waffle Charts in R to Analyze Visits to the Grand Canyon - Real Life <- Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s