- Layout – mainly handles by grid package.
- 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.
We aim to obtain the following layout and design in the final output of our code:
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:
Table 1: Colours Used for in the Infographic.
|Gray (Infographic Text)|
|Dark Yellow (Crime Text)|
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,
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
dat data frame, this time using the line plot.
Obtain by running the following code:
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
y3 variable is plotted using the following codes:
by default we have,
All plots are now set, next is to place it in the layout. The following steps explain the procedure:
- Start by creating new grid plot,
- 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
- Next is the background colour, this will be the background colour of the infographic. For the given chart, we run the following:
- Next is to insert texts in the layout, use the
grid.textfunction. 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$;
- To insert the plot into a specific window in the matrix plot use the
vplayoutfunction for the coordinates of the placeholder, and
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!