The goal of this repository is to keep track of some neat ggplot2 tricks I’ve learned. This assumes you’ve familiarised yourself with the basics of ggplot2 and can construct some nice plots of your own. If not, please peruse the book at your leasure.
I’m not incredibly adapt in gloriously typesetting plots and expertly
finetuning themes and colour palettes, so you’d have to forgive me. The
mpg dataset is very versatile for plotting, so you’ll be seeing a lot
of that as you read on. Extension packages are great, and I’ve dabbled
myself, but I’ll try to limit myself to vanilla ggplot2 tricks here.
For now, this will be mostly a README-only bag of tricks, but I may decide later to put them into separate groups in other files.
By loading the library and setting a plotting theme. The first trick
here is to use theme_set() to set a theme for all your plots
throughout a document. If you find yourself setting a very verbose theme
for every plot, here is the place where you set all your common
settings. Then never write a novel of theme elements ever again[1]!
library(ggplot2)
library(scales)
ggplot(data = cars) +
aes(x = speed, y = dist) +
geom_point() ->
p; p

theme_set(
# Pick a starting theme
theme_gray() +
# Add your favourite elements
theme(
axis.line = element_line(),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line("grey95", linewidth = 0.25),
legend.key = element_rect(fill = NA)
)
)
# returning to plot
p

The ?aes documentation doesn’t tell you this, but you can splice the
mapping argument in ggplot2. What does that mean? Well it means that
you can compose the mapping argument on the go with !!!. This is
especially nifty if you need to recycle aesthetics every once in a
while.
library(ggplot2)
my_xy_mapping <- aes(x = speed, y = dist)
p <- ggplot(data = cars) +
aes(color = speed > 15,
!!!my_xy_mapping) +
geom_point(size = 6)
# also...
q <- ggplot(data = cars) +
aes(color = speed > 15) +
my_xy_mapping +
geom_point(size = 6)
identical(p,q)
#> [1] FALSE
p

q

#waldo::compare(p,q)
My personal favourite use of this is to make the fill colour match the
colour colour, but slightly lighter[2]. We’ll use the delayed
evaluation system for this, after_scale() in this case, which you’ll
see more of in the section following this one. I’ll repeat this trick a
couple of times throughout this document.
ggplot(mpg, aes(displ, hwy)) +
geom_point(shape = 21) +
aes(colour = factor(cyl)) +
# fill directly using variable
aes(fill = hwy) +
# fill based on existing scale color
aes(fill = after_scale(alpha(colour, 0.3))) ->
p;p

# or reusably
aes(fill = after_scale(alpha(colour, 0.3))) ->
aes_fill_transparent_after_color
# aes can be applied independently so following also works
ggplot(mpg) +
aes(x = displ, y = hwy) +
geom_point(shape = 21) +
aes_fill_transparent_after_color +
aes(colour = factor(cyl))

You may find yourself in a situation wherein you’re asked to make a heatmap of a small number of variables. Typically, sequential scales run from light to dark or vice versa, which makes text in a single colour hard to read. We could devise a method to automatically write the text in white on a dark background, and black on a light background. The function below considers a lightness value for a colour, and returns either black or white depending on that lightness.
Now, we can make an aesthetic to be spliced into a layer’s mapping
argument on demand.
Lastly, we can test out our automatic contrast contraption. You may notice that it adapts to the scale, so you wouldn’t need to do a bunch of conditional formatting for this.
contrast <- function(colour) {
out <- rep("grey20", length(colour))
light <- farver::get_channel(colour, "l",
space = "hcl")
out[light < 50] <- "grey80"
out
}
aes(colour = after_scale(contrast(fill))) ->
aes_colour_autocontrast_after_fill
library(tidyverse)
#> ── Attaching core tidyverse packages ─────────────────── tidyverse 2.0.0.9000 ──
#> ✔ dplyr 1.1.0 ✔ readr 2.1.4
#> ✔ forcats 1.0.0 ✔ stringr 1.5.0
#> ✔ lubridate 1.9.2 ✔ tibble 3.2.1
#> ✔ purrr 1.0.1 ✔ tidyr 1.3.0
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ readr::col_factor() masks scales::col_factor()
#> ✖ purrr::discard() masks scales::discard()
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
cor(mtcars) |>
as.data.frame() |>
tibble::rownames_to_column("row") |>
pivot_longer(-row, names_to = "col") |>
ggplot(aes(row, col)) +
geom_raster() +
aes(fill = value) +
geom_text(aes(label = round(value, 2)), size = 3) +
coord_equal() +
aes_colour_autocontrast_after_fill +
scale_fill_viridis_c(direction = 1) +
scale_fill_viridis_c(direction = -1)
#> Scale for fill is already present.
#> Adding another scale for fill, which will replace the existing scale.

There are some extensions that offer half-geom versions of things. Of the ones I know, gghalves and the see package offer some half-geoms.
Here is how to abuse the delayed evaluation system to make your own. This can come in handy if you’re not willing to take on an extra dependency for just this feature.
The easy case is the boxplot. You can either set xmin or xmax to
after_scale(x) to keep the right and left parts of a boxplot
respectively. This still works fine with position = "dodge".
# A basic plot to reuse for examples
mpg |>
ggplot() +
aes(class, displ, colour = class) +
geom_boxplot() +
aes(xmin = after_scale(x)) +
# make it pretty
aes_fill_transparent_after_color +
labs(y = "Engine Displacement [L]", x = "Type of car") +
guides(colour = "none", fill = "none")

The same thing that works for boxplots, also works for errorbars.
mpg |>
ggplot() +
aes(class, displ, colour = class) +
geom_errorbar(stat = "summary",
fun.data = mean_se) +
aes(xmin = after_scale(x))
#> Warning: Failed to apply `after_scale()` modifications to legend
#> Caused by error in `after_scale()`:
#> ! object 'x' not found

We can once again do the same thing for violin plots, but the layer
complains about not knowing about the xmin aesthetic. It does use that
aesthetic, but only after the data has been setup, so it is not
intended to be a user accessible aesthetic. We can silence the warning
by updating the xmin default to NULL, which means it won’t complain,
but also doesn’t use it if absent.
mpg |>
ggplot() +
aes(class, displ, colour = class) +
geom_violin() +
aes(xmin = after_scale(x)) ->
p
update_geom_defaults("violin", list(xmin = NULL))
p
#> Warning: Failed to apply `after_scale()` modifications to legend
#> Caused by error in `after_scale()`:
#> ! object 'x' not found

Not left as an exercise for the reader this time, but I just wanted to show how it would work if you were to combine two halves and want them a little bit offset from one another. We’ll abuse the errorbars to serve as staples for the boxplots.
# A small nudge offset
offset <- 0.025
# Combining
mpg |>
ggplot() +
aes(class, displ, colour = class) +
geom_boxplot(aes(xmax = after_scale(x))) +
geom_errorbar(stat = "boxplot", width = 0.3,
aes(xmax = after_scale(x))) +
aes(x = stage(class, after_stat = x - offset)) +
geom_violin(aes(
xmin = after_scale(x),
x = stage(class, after_stat = x + offset)))
#> Warning: Failed to apply `after_scale()` modifications to legend
#> Failed to apply `after_scale()` modifications to legend
#> Failed to apply `after_scale()` modifications to legend
#> Caused by error in `after_scale()`:
#> ! object 'x' not found

Let’s say you have better colour intuition than I have, and three
colours aren’t enough for your divergent colour palette needs. A
pain-point is that it is tricky to get the midpoint right if your limits
aren’t perfectly centered around it. Enter the rescaler argument in
league with scales::rescale_mid().
my_pal <- c("dodgerblue", "deepskyblue", "grey", "hotpink", "deeppink")
ggplot(mpg) +
aes(displ, hwy, colour = cty - mean(cty)) +
geom_point() +
labs(
x = "Engine displacement [L]",
y = "Highway miles per gallon",
colour = "Mean-\ncentered\nvalue"
) +
scale_colour_gradientn(
colours = my_pal,
) +
scale_colour_gradientn(
colours = my_pal,
rescaler = ~ scales::rescale_mid(.x, mid = 0) # opt 1 center
) +
scale_colour_gradientn(
colours = my_pal,
limits = ~ c(-1, 1) * max(abs(.x)) # opt 2 center limits
)
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.
#> Scale for colour is already present.
#> Adding another scale for colour, which will replace the existing scale.

You can label points with geom_text(), but a potential problem is that
the text and points overlap.
set.seed(0)
df <- USArrests[sample(nrow(USArrests), 5), ]
df$state <- rownames(df)
ggplot(df) +
aes(Murder, Rape, label = state) +
geom_point() +
geom_text()

last_plot() +
aes(hjust = Murder > mean(Murder),
vjust = Rape > mean(Rape))

ggwipe::last_plot_wipe_last() +
aes(hjust = NULL, vjust = NULL) +
geom_text(nudge_x = 1, nudge_y = 1)

ggwipe::last_plot_wipe_last() +
geom_label(
label.padding = unit(5, "pt")
)

last_plot() +
aes(label = gsub(" ", "\n", state),
hjust = Murder > mean(Murder),
vjust = Rape > mean(Rape))

ggwipe::last_plot_wipe_last() +
geom_label(
label.padding = unit(5, "pt"),
label.size = NA,
fill = NA
)

There are several typical solutions to this problem, and they all come with drawbacks:
nudge_x and nudge_y parameters. The issue here
is that these are defined in data units, so spacing is
unpredictable, and there is no way to have these depend on the
original locations.hjust and vjust aesthetics. It allows you to
depend on the original locations, but these have no natural offsets.Here are options 2 and 3 in action:
q + geom_text(nudge_x = 1, nudge_y = 1)
q + geom_text(aes(
hjust = Murder > mean(Murder),
vjust = Rape > mean(Rape)
))
You might think: ‘I can just multiply the justifications to get a wider offset’, and you’d be right. However, because the justification depends on the size of text you might get unequal offsets. Notice in the plot below that ‘North Dakota’ is offset too munch in the y-direction and ‘Rhode Island’ in the x-direction.
q + geom_text(aes(
label = gsub("North Dakota", "North\nDakota", state),
hjust = ((Murder > mean(Murder)) - 0.5) * 1.5 + 0.5,
vjust = ((Rape > mean(Rape)) - 0.5) * 3 + 0.5
))
The nice thing of geom_label() is that you can turn off the label box
and keep the text. That way, you can continue to use other useful stuff,
like the label.padding setting, to give an absolute (data-independent)
offset from the text to the label.
q + geom_label(
aes(
label = gsub(" ", "\n", state),
hjust = Murder > mean(Murder),
vjust = Rape > mean(Rape)
),
label.padding = unit(5, "pt"),
label.size = NA, fill = NA
)
This used to be a tip about putting facet tags in panels, which used to
be complicated. With ggplot2 3.5.0, you no longer have to fiddle with
setting infinite positions and tweaking the hjust or vjust
parameters. You can now just use x = I(0.95), y = I(0.95) to place
text in the upper-right corner. Open up the details to see the old tip.
Unfortunately, this places the text straight at the border of the panel,
which may offend our sense of beauty. We can get slightly fancier by
using `geom_label()`, which lets us more precisely control the spacing
between the text and the panel borders by setting the `label.padding`
argument.
Moreover, we can use `label.size = NA, fill = NA` to hide the textbox
part of the geom. For illustration purposes, we now place the tag at the
top-left instead of top-right.
``` r
p + facet_wrap(~ class, scales = "free") +
geom_label(
data = ~ subset(.x, !duplicated(class)),
aes(x = -Inf, y = Inf, label = LETTERS[seq_along(class)]),
hjust = 0, vjust = 1, label.size = NA, fill = NA,
label.padding = unit(5, "pt"),
colour = "black"
)
#> Warning: Failed to apply `after_scale()` modifications to legend
#> Caused by error in `after_scale()`:
#> ! object 'x' not found
```
Let’s say we’re tasked with making a bunch of similar plots, with different datasets and columns. For example, we might want to make a series of barplots[4] with some specific pre-sets: we’d like the bars to touch the x-axis and not draw vertical gridlines.
One well-known way to make a bunch of similar plots is to wrap the plot construction into a function. That way, you can use encode all the presets you want in your function.
I case you might not know, there are various methods to program with
the aes()
function,
and using `` (curly-curly) is one of the more flexible ways [5].
barplot_fun <- function(data, x) {
ggplot(data, aes(x = )) +
geom_bar(width = 0.618) +
scale_y_continuous(expand = c(0, 0, 0.05, 0)) +
theme(panel.grid.major.x = element_blank())
}
barplot_fun(mpg, class)

One drawback of this approach is that you lock-in any aesthetics in the
function arguments. To go around this, an even simpler way is to simply
pass ... directly to aes().
barplot_fun <- function(data, ...) {
ggplot(data, aes(...)) +
geom_bar(width = 0.618) +
scale_y_continuous(expand = c(0, 0, 0.1, 0)) +
theme(panel.grid.major.x = element_blank())
}
barplot_fun(mpg, class, colour = factor(cyl), !!!aes_fill_transparent_after_color)

Another method of doing a very similar thing, is to use plot
‘skeletons’. The idea behind a skeleton is that you can build a
plot, with or without any data argument, and add in the specifics
later. Then, when you actually want to make a plot, you can use the
%+% to fill in or replace the dataset, and + aes(...) to set the
relevant aesthetics.
barplot_skelly <- ggplot() +
geom_bar(width = 0.618) +
scale_y_continuous(expand = c(0, 0, 0.1, 0)) +
theme(panel.grid.major.x = element_blank())
my_plot <- barplot_skelly %+% mpg +
aes(class, colour = factor(cyl),
!!!aes_fill_transparent_after_color)
my_plot

One neat thing about these skeletons is that even when you’ve already
filled in the data and mapping arguments, you can just replace them
again and again.
my_plot %+%
mtcars +
aes(factor(carb), colour = factor(cyl),
!!!aes_fill_transparent_after_color)

The idea here is to not skeletonise the entire plot, but just a
frequently re-used set of parts. For example, we might want to label our
barplot, and pack together all the things that make up a labelled
barplot. The trick to this is to not add these components together
with +, but simply put them in a list(). You can then + your list
together with the main plot call.
labelled_bars <- list(
geom_bar(aes_fill_transparent_after_color,
width = 0.618),
geom_text(
stat = "count",
aes(y = after_stat(count),
label = after_stat(count),
fill = NULL, colour = NULL),
vjust = -1, show.legend = FALSE
),
scale_y_continuous(expand = c(0, 0, 0.1, 0)),
theme(panel.grid.major.x = element_blank())
)
ggplot(mpg, aes(class, colour = factor(cyl))) +
labelled_bars +
ggtitle("The `mpg` dataset")
ggplot(mtcars, aes(factor(carb), colour = factor(cyl))) +
labelled_bars +
ggtitle("The `mtcars` dataset")


Well, you need to do it once at the start of your document. But then
never again! Except in your next document. Just write a
plot_defaults.R script and source() that from your document.
Copy-paste that script for every project. Then, truly, never again
:heart:.
This is a lie. In reality, I use aes(colour =
after_scale(colorspace::darken(fill, 0.3))) instead of lightening
the fill. I didn’t want this README to have a dependency on
{colorspace} though.
Unless you self-sabotage your plots by setting oob =
scales::oob_censor_any in the scale for example.
In your soul of souls, do you really want to make a bunch of barplots though?
The alternative is to use the .data pronoun, which can be
.data$var if you want to lock in that column in advance, or
.data[[var]] when var is passed as a character.
This bit was originally called ‘partial skeleton’, but as a ribcage is a part of a skeleton, this title sounded more evocative.