solarized_theme_ggthemr

Theming ggplot figure output

The default colour themes in ggplot2 are beautiful. Your figures look great, the colours match, and you have the characteristic “R” look and feel. The author of ggplot2, Hadley Wickham, has done a fantastic job.

For the tinkerers, there’s methods to change every part of the look and feel of your figures. In practice however, changing all of the defaults can feel laborious and too much work when you want a quick change to look and feel.

The ggthemr package was developed by a friend of mine, Ciarán Tobin, who works with me at KillBiller and Edgetier. The package gives a quick and easy way to completely change the look and feel of your ggplot2 figures, as well as quickly create a theme based on your own, or your company’s, colour palette.

In this post, we will quickly examine some of the built in theme variations included with ggplot2 in R, and then look at the colour schemes available using ggthemr.

Basic themes included in ggplot2

There’s 8 built-in theme variations in the latest versions of ggplot2. Quickly change the default look of your figures by adding theme_XX() to the end of your plotting commands.

Let’s look at some of these. First create a simple figure, based on the massively overused “iris” dataset, since it’s built-in to R.

The default look and feel can be adjusted by adding an in-build theme from ggplot2.

  • theme_gray() – signature ggplot2 theme
  • theme_bw() – dark on light ggplot2 theme
  • theme_linedraw() – uses black lines on white backgrounds only
  • theme_light() – similar to linedraw() but with grey lines aswell
  • theme_dark() – lines on a dark background instead of light
  • theme_minimal() – no background annotations, minimal feel.
  • theme_classic() – theme with no grid lines.
  • theme_void() – empty theme with no elements

Examples of these theme’s applied to the figure is shown below.

Note that the colour palette for your figures is not affected by these theme changes – only the figure parameters such as the grid lines, outlines, and backgrounds etc.

The “ggthemr” package

The ggthemr R package is an R package to set up a new theme for your ggplot figures, and completely change the look and feel of your figures, from colours to gridlines. The package is available on github, and is installed using the Devtools package:

The ggthemr package is built for “fire-and-forget” usage: you set the theme at the start of your R session, and all of your plots will look different from there.

The command to set a theme is:

For example:

pointplot-dust.pngbarplot_dust.png

“ggthemr” colour themes

There are 17 built-in colour themes with ggthemr, each one providing a unique way to change your ggplot figure look. All are listed on the ggthemr github page. The colour themes available built-in are:

  • flat
  • flat-dark
  • camoflauge
  • chalk
  • copper
  • dust
  • earth
  • fresh
  • grape
  • grass
  • greyscale
  • light
  • lilac
  • pale
  • sea
  • sky
  • solarized

One benefit of using ggthemr is that the default color palettes are replaced for lines and bars – completely changing the look of the charts. A complete view of all themes is available here, and some random examples are shown below.

theme_dust.png

“Dust” look and feel across charts

theme_earth.png

“Earth” look and feel colour palette for box plot, scatter diagram, and bar chart.

theme_flat.png

“Flat” look and feel from ggthemr for various chart types

Custom palettes with ggthemr

If you’re in a working environment that has it’s own custom palette, for example a consultancy or accounting firm, it’s great to have ggplot2 figures match your document templates and power point files.

Ggthemr allows you to specify a custom theme palette that can be applied easily. Imagine we worked for “Tableau“, the data visualisation and business intelligence platform.

To define the custom theme, get the colour scheme for tableau figures in hex, choose a base theme, then define the swatch for ggthemr:

Along with the swatch, gradient, and background elements of the figures, define_palette() also accepts specification of the figure text colours, line colours, and minor and major gridlines.

Additional figure controls

There are three additional controls in the ggthemr package allowing further figure adjustments:

  • “Type” controls whether the background colour spills over the entire plot area, or just the axes section, options inner or outer
  • “Spacing” controls the padding between the axes and the axis labels / titles, options 0,1,2.
  • “Layout” controls the appearance and position of the axes and gridlines – options clean, clear, minimal, plain, scientific.

For example, a new figure look can be created with:

other_earth_options.png

“Earth” themed geom_point() with a scientific layout, extra spacing, and outer type specified for ggthemr.

Manually customising a ggplot2 theme

If ggthemr isn’t doing it for you, the in-built ggplot2 theming system is completely customisable. There’s an extensive system in ggplot2 for changing every element of your plots – all defined using the theme() function. For example, the theme_grey() theme is defined as:

To create a theme of your own – you can change the values in this function definition, and add it to your plot as so:

As you can see, its not for the faint hearted, so for your quick-fix, I’d recommend looking at ggthemr and wish you the best with beautiful plots in your next presentation! Let us know how things go!

  1. Fantastic !

    Question: could you add an arrow at end of x axis and at the top of y axis? I need this for academic purposes, and yet simple end of line arrows are non existent in ggplot2. Thanks in advance for update of this article.

Leave a Reply