R: How to Layout and Design an Infographic

[This article was first published on Analysis with Programming, 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.

As promised from my recent article, here’s my tutorial on how to layout and design an infographic in R. This article will serve as a template for more infographic design that I plan to share on future posts. Hence, we will go through the following sections:
  1. Layout – mainly handles by grid package.
  2. Design – style of the elements in the layout.
    • Texts – use extrafont package for custom fonts;
    • Shapes (lines and point characters) – use grid, although this package has been removed from CRAN (as of February 26, 2015), the compressed file of the source code of the package is still available. But if I am not mistaken, by default this package is included in R. You might check it first before installing.
    • Plots – several choices for plotting data in R: base plot, lattice, or ggplot2 package.

The Infographic

We aim to obtain the following layout and design in the final output of our code:
To start with, we need to setup our data first. And for illustration purposes, we will use a simulated data:

Colour

The aesthetic of an infographic not only depends on the shapes and plots, but also on the colours. So if you are not an artist, I suggest to look first for a list of sample infographics to get some inspiration. Once you have found the theme for your charts, grab the colour of it. To grab the colour, use eyedropper tool from software such as photoshop, affinity designer, etc. There is also free add ons for Mozilla Firefox called ColorZilla, I haven’t tried it but maybe you could explore that. For the above theme, there are five colours with the following hexadecimal colour code:

Colour NameHexadecimal
Table 1: Colours Used for in the Infographic.
Dark Violet#552683
Dark Yellow#E7A922
White#FFFFFF
Gray (Infographic Text)#A9A8A7
Dark Yellow (Crime Text)#CA8B01

Data Visualization

At this point, we’ll prepare the elements in the layout, and we begin with the plots. Below is the bar plot of y1 in the data frame, dat, in three groupings, grp. Note that the plot you’ll obtain will not be the same with the one below since the data changes every time we run the simulation above.
So that’s the default theme of ggplot2, and we want to customize this using the theme function. One of the elements in the plot that will be tweaked is the font. To deal with this we need to import the fonts using the extrafont package. That is,

What happens above is that all fonts installed in your machine will be imported. It’s better to import all of it so that we’ll have several choices to play on. For the above infographic, the font used is called Impact, which is available on windows and I think on mac as well. If you don’t have that, then download and install it first before running the above codes. To arrive on the design of the bar plot in the infographic we use the following theme,

I named it kobe_theme since if you recall from my previous article, the above chart is inspired by Kobe Bryant Infographic. So applying this to the plot we’ll have the following,
Obtain by running p1 + kobe_theme(). If in case you want to reorder the ticks in the x-axis, by starting with A from the top and ending with L in the bottom, simply run the following,

And you’ll have
So that’s our first plot, next is to plot y2 from dat data frame, this time using the line plot.
Obtain by running the following code:

Applying kobe_theme, will give us
Above plot is generated by running p2 + kobe_theme(). We should expect this since the kobe_theme that was applied in the bar plot with coord_flip option enabled, affects the orientation of the grids. So instead, we do a little tweak on the current theme, and see for yourself the difference:

So that we have the following result:
Now that’s better, one more issue is the title label for the legend. To change the label, run the following code:

And that will give us
Finally, y3 variable is plotted using the following codes:

by default we have,
Applying kobe_theme2(),

Layout

All plots are now set, next is to place it in the layout. The following steps explain the procedure:
  1. Start by creating new grid plot, grid.newpage();
  2. Next define the layout of the grid. Think of this as a matrix of plots, where a 2 by 2 matrix plot will give us 4 windows (two rows and two columns). This windows will serve as a placeholder of the plots. So to achieve a matrix plot with 4 rows and 3 columns, we run

  3. Next is the background colour, this will be the background colour of the infographic. For the given chart, we run the following:

  4. Next is to insert texts in the layout, use the grid.text function. The position of objects/elements such as texts in the grid is defined by the (x, y) coordinates. The bound of the grid by default is a unit square, of course the aspect ratio of the square can be modified. So the support of x and y is $[0,1]^2$;
  5. To insert the plot into a specific window in the matrix plot use the vplayout function for the coordinates of the placeholder, and print for pasting. Say we want to insert the first plot in first row, second column, we code it this way

    Now to place it in first row and stretched it over all (three) columns, run

Using the above procedure, we have the following codes for the infographic. Enjoy!

PNG Output



PDF Output


To leave a comment for the author, please follow the link and comment on their blog: Analysis with Programming.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)