3MW (Remove annoying white space in ggplot)

Guten Tag!

Many greetings from Ulm, Germany. Like every week, let me share with you my newest video tutorial before we get started with this week’s newsletter.

Want to build a custom legend without too much hardcoding? Last week, I've shown you how to build a neat custom color legend in ggplot. But it hardcoded too many things. So this week's new video shows you a better way by going through the changes one by one.

Have you ever noticed that ggplot uses weird spacing in bar charts? Have a look at this bar chart.

You can clearly see that the labels are not directly adjacent to the bars. Instead, there is some slightly annoying white space in there.

What you need to do to fix this, is to get rid of the so-called axes expansions. You see, ggplot usually checks the range of values of the data that you map to an axis and then it expands that range a little bit. For charts like scatter plots, this gives us a nicer look because points are not located at the edge of the plotting area this way.

So,this is why ggplot does it. But with bar charts this is not particularly useful. Therefore, let us remove this (you will find all of today’s code on GitHub).

A brute-force fix

We start out with a basic bar chart. Here's the code that creates the plot from above.

What we could do is to add a coord_cartesian() layer and set expand = FALSE in there. This turns off all axes expansions and you can see that we got rid of the weird spacing between the manufacturer labels and the bars. So that would be an easy fix. Let’s have a look if this works.

Unfortunately, we also got rid of all our extra spacing that added a little bit of padding above and below the bars as well as to the right of the bars. So, right now our bars go all the way to the end of our plotting area and this may feel a bit a little bit squeezed. We should do something else instead.

A fine-grained solution

Let's remove the coord_cartesian() layer and, instead, let us target only the x-axis via the scale_x_continuous() layer. In there, we can set the expansion argument to manually change how much of an axis expansion we want. What we're going for here is no expansion on the left side of the axis and a little expansion on the right side.

To make that happen, we use the expansion() helper function. This function is a really simple one. It always returns a vector of length 4.

These four values correspond to the

  • additive and multiplicative expansion of the

  • left and right side of the axis that we target

What this means is that for each side of the axis, we can either

  • add a fixed amount of extra spacing (that's the additive part) and

  • use a scaling factor to stretch the axis (that's the multiplicative part)

You can set these things with mult and add in expansion():

As you can see, all that the expansion() function does, is to take your inputs from mult and add and arrange these values differently. Basically, this order is the order that ggplot expects but it's not necessarily easy to remember. Hence, this helper function makes it easier for you.

You see, in both mult and add now the two numbers simply correspond to the lower end and upper end of the axis (in that order). So, for an x-axis that's left and right. And for a y-axis that's down and up.

Coming back to our bar chart, once we understand how to how these expansions work you just have to tweak the values in expansion() to do what you want. Here, we don't want to have any fixed expansions, so we just ignore the add argument. But we want to have, say, 5% expansion on the right so we set mult = c(0, 0.05).

As always, there is no magic formula to choose the value that we've used for expanding here. You just have to play around with it until you're satisfied with the look of the image.

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, uhhh I mean X.

See next week,
Albert 👋

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

Join the conversation

or to participate.