Site icon R-bloggers

Infographic-style charts using the R waffle package

[This article was first published on R – What You're Doing Is Rather Desperate, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 extra. I’ve just made it a little more step-by-step.

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

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

library(waffle)
library(extra)

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 s directory. To install the , use the file named awesome-web.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 s
In R, s have to be imported to the extra database. You need to do this whenever a new is installed that you want to use in R.

_import()

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

If you run _import() in a session, you need to register the s 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
loads()
# use this if things look odd in RStudio under Windows
loads(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.


Filed under: R, statistics Tagged: packages, rstats, visualization, waffle

To leave a comment for the author, please follow the link and comment on their blog: R – What You're Doing Is Rather Desperate.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.