Cyberhelp

for Researchers & Teams

ggplot tricks not to forget about

September 25, 2019 by

Tweaking figures for presentations or publications can be a tedious process, especially when I always need a reminder on “how to use greek letters or subscripts in y-axis”, “remove legend”, and “r pch”. Here are a collection of some ggplot2 functions and arguments that I find particularly useful and want to remember.

Getting oriented with proper axes

Within an xlab() or ylab() function, use expression(paste()) to use special characters. Use brackets ([]) for subscript, the caret (^) for superscript, and the names of greek letters e.g. mu.

  ylab(expression(paste("C", H[4], " (", mu,"mol ", L^-1,")"))) +
  xlab(expression(paste("DOC (mg ", L^-1,")")))

Show me the data!

  scale_shape_manual(values = c(21, 24), 
                     name = element_blank(),
                     labels = c("Landsat", "Sentinel2"))

Special text and labels

set.seed(222)
mydata <- data.frame(x = 1:10, y = 1:10 + rnorm(10, sd = 2))
fit <- lm(y ~ x, data = mydata)
rsq_label <- paste('R^2 == ', round(summary(fit)$r.squared, 2))

ggplot(mydata, aes(x = x, y = y)) +
  geom_point(size = 2) +
  geom_smooth(method = 'lm', se = FALSE, size = 1.5) +
  annotate(geom = 'text', x = -Inf, y = Inf, label = rsq_label, hjust = 0, vjust = 1, parse = TRUE)

## `geom_smooth()` using formula 'y ~ x'

mydata <- data.frame(y = 1:10, variable = c('Flux~(g~m^-2~s^-1)', 'Mass~(g)'))
ggplot(mydata, aes(y = y)) + 
  geom_boxplot() +
  facet_wrap(~ variable, labeller = label_parsed)

Putting it all together

The basic idea is to initiate an empty drawing canvas with the ggdraw() function, and then determined the location and sizing of each panel with draw_plot() functions.

If you have 4 ggplot objects called panel_a, panel_b, panel_c, and panel_d, creating a 4-panel figure with labels would look something like this:

figure <- ggdraw() +
  draw_plot(panel_a, x = 0, y = .5, width = 0.4, height = 0.5) +
  draw_plot(panel_b, x = .4, y = .5, width = 0.6, height = 0.5) +
  draw_plot(panel_c, x = 0, y = 0, width = 0.4, height = 0.5) +
  draw_plot(panel_d, x = .4, y = 0, width = 0.6, height = 0.5) +
  draw_plot_label(label = c("a", "b", "c", "d"), x = c(0, 0.4, 0, 0.4), y = c(1,1, 0.5, 0.5), size = 15)

The arguments for the draw_plot function are:

There is also a handy function for saving this type of plot to a file:

save_plot("figure.png", figure, ncol = 2, nrow = 2, base_aspect_ratio = 1.3)

As I haven’t determined anything particulary bovine-related in the package, I’m pretty sure the name references the author’s initials.

The best one I have found is written by Jon Lefcheck and can be sourced directly from GitHub:

source('https://gist.githubusercontent.com/jslefche/eff85ef06b4705e6efbc/raw/736d3dc9fe71863ea62964d9132fded5e3144ad7/theme_black.R')

mydata <- data.frame(x=runif(100), y=runif(100), gp=letters[1:5])
ggplot(mydata,aes(x = x,y = y, color = gp)) + 
  geom_line() + 
  theme_black()

Related