`grattan_richlegend()` places a coloured, on-chart legend at one of the panel corners (or a custom position). A white background box sits behind the text so the labels remain readable when they overlap gridlines, in the same no-border style as [grattan_label()]. Inspired by `ggdirectlabel::geom_richlegend()`.

grattan_richlegend(
  mapping = NULL,
  data = NULL,
  legend.position = "topright",
  size = 18,
  padding = 0.2,
  lineheight = 0.8,
  fill = "white",
  na.rm = FALSE,
  inherit.aes = TRUE,
  ...
)

Arguments

mapping

Aesthetic mapping created by [ggplot2::aes()]. Must include `label` and `colour`.

data

Data for the layer.

legend.position

Where to place the legend. Either one of `"topright"`, `"topleft"`, `"bottomright"`, `"bottomleft"`, `"top"`, `"bottom"`, `"left"`, `"right"`, or a numeric vector of length 2 with values between 0 and 1 giving the relative `(x, y)` position within the panel. Defaults to `"topright"`.

size

Font size for legend text. Default is `18`.

padding

Amount of white padding around the legend, measured in "lines". Default is `0.2`.

lineheight

Line height between legend entries. Default is `0.8`.

fill

Background fill colour. Default is `"white"`.

na.rm

If `FALSE` (default) missing values are removed with a warning.

inherit.aes

If `FALSE`, overrides the default aesthetics rather than combining with them.

...

Other arguments passed to [ggplot2::layer()].

Value

A ggplot2 layer.

Examples

if (FALSE) { # \dontrun{
library(ggplot2)

df <- data.frame(
  year = rep(2000:2010, 3),
  value = c(seq(1, 11), seq(2, 12), seq(3, 13)),
  series = rep(c("A", "B", "C"), each = 11)
)

ggplot(df, aes(year, value, colour = series)) +
  geom_line() +
  grattan_richlegend(aes(label = series)) +
  theme_grattan()
} # }