3MW (Create your own ggplot theme)

Guten Tag!

Many greetings from Ulm, Germany. As always, a short announcement before we dive into this week’s newsletter:

As promised, I’ve released Part 2 of my Quarto styling guide. You can find it on YouTube.

Speaking about style guides, I’ve realized that I always use the same set of theme changes in {ggplot2}. If you’ve been meaning to create your own theme, these changes may be a great start.

So, let me walk you through my most common theme changes. As always, you can find this week’s code on GitHub.

A starting point

Let us begin with a simple scatter plot that comes in {ggplot2}’s default look. Here’s how it looks.

And here’s the code for this. Notice that I like to use the subtitle instead of the y aesthetic to describe what’s on the y-axis. This is somewhat opinionated and I do this for reading comfort (i.e. no weird head tilting). You’ll have to make sure that it’s recognizable as an axis label by adding some spacing to the subtitle (more on that later)

Add theme_minimal()

The first thing I do on any plot is add theme_minimal().

This strips the grey background which I personally perceive as clutter.

Increase font size and use nice font family

But I don’t leave theme_minimal() just like that. I think it is crucial to change the base_size in theme_minimal(). Crank it up until the font is large enough so that everyone can read it really easily. And while you’re at it, why not also set base_family to a nice font like “Source Sans Pro”.

Use other colors

Personally, I can’t stand the default colors anymore. I’ve just seen them too often. And when I see them, I automatically think that the chart’s creator hasn’t put any effort into a (possibly) better choice.

I like to use the colorblind-safe Okabe Ito color palette. And the easiest way to get the colors’ hex codes is using okabe_ito() from {thematic} (which you might have installed anyway).

Alternatively, you can just use any other hex codes that you get from e.g. Coolors.

And here’s a pro tip: You can also specify which category gets which color.

Remove superfluous grid lines

The default theme_minimal() comes with waaay too many grid lines (clutter in my opinion). You can remove a good part of them by setting panel.grid.minor = element_blank() in theme().

Make grid lines less noticeable

You can also make the remaining grid lines less noticeable so that they are still visible but don’t take up as much visual space. For example, you could make them into dashed, light-grey lines.

Just change the line properties in panel.grid.major with element_line() to make this happen.

Move titles & captions

By default, the plot titles align with the inner panel. I like to have them aligned to the left of the chart like so

This is handled by setting plot.title.position to 'plot'.

The same thing can be done for the captions with plot.caption.position='plot'.

Make title flashy

I like to make the chart title very large and use a different font like so:

The corresponding code changes the text properties of plot.title with element_text(). Pro tip: Set the size as something relative to the base size with rel().

Give y-axis label some room

As discussed, we need to separate title and subtitle a bit. For example, you could add some margin at the bottom of the plot title with margin().

Adjust subtitle size

Just like the plot title size, the y-axis label could be a bit bigger. After all, what is depicted on the y-axis is often what we care about most.

Optional: Lighter texts and frame

Often, what I like to do is to change the text colors from regular black to a dark grey. Also, I like to put a frame around the panel. The latter can be done by changing the color (not the fill) of the panel’s rectangle with element_rect().

 

Reusable function

If you want to reuse these changes, you could just throw all of this into a function like so:

That’s it for today. Hope you’ve enjoyed this week’s newsletter. If you want to reach out to me, just reply to this mail or find me on Twitter.

See next week,
Albert 👋

If you like my content, you may also enjoy these:

Reply

or to participate.