Powered by AppSignal & Oban Pro

Custom annotations: annotation_custom

notebooks/annotation_custom.livemd

Custom annotations: annotation_custom

Mix.install([
  {:giggly, path: Path.join(__DIR__, "..")},
  {:kino, "~> 0.19"}
  ])

About this notebook

This notebook follows the examples on ggplot2's reference page for annotation_custom(), in the order the page gives them. Each example shows ggplot2's code first and then Giggly's. An example Giggly cannot draw yet keeps its ggplot2 code and names what it is waiting for, so the page is also a list of what remains.

annotation_custom() draws a grid grob, one of the graphical objects R's grid graphics are made of, such as a table or an inset plot, in a rectangle given in data coordinates, the same in every panel. It does not change the scales, so the axes do not grow to fit it, and no setting or mapping of the plot changes it; -Inf and Inf stretch it to the panel's edges. Giggly has none of it yet.

A grob over the whole panel

# Dummy plot
df <- data.frame(x = 1:10, y = 1:10)
base <- ggplot(df, aes(x, y)) +
  geom_blank() +
  theme_bw()

# Full panel annotation
base + annotation_custom(
  grob = grid::roundrectGrob(),
  xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf
)

geom_blank() draws nothing, but its data still sets the scales.

Not in Giggly yet: geom_blank(), annotation_custom(), and grid grobs.

An inset plot

# Inset plot
df2 <- data.frame(x = 1 , y = 1)
g <- ggplotGrob(ggplot(df2, aes(x, y)) +
  geom_point() +
  theme(plot.background = element_rect(colour = "black")))
base +
  annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)

ggplotGrob() turns a whole plot into a grob, to be drawn inside another.

Not in Giggly yet: ggplotGrob() and grid grobs, annotation_custom(), and the geom_blank() of base.