Powered by AppSignal & Oban Pro

Custom guides: guide_custom

notebooks/guide_custom.livemd

Custom guides: guide_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 guide_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.

guide_custom() draws any graphical object, a grid grob, alongside a plot's legends, with a title if given one. It belongs to no scale. Giggly has none of it yet: it has no guide_custom() and no grid grobs, and draws only the legends and colour bars its alpha, colour and fill scales ask for.

A grob as a guide

# A standard plot
p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()

# Define a graphical object
circle <- grid::circleGrob()

# Rendering a grob as a guide
p + guides(custom = guide_custom(circle, title = "My circle"))

Not in Giggly yet: grid grobs and guide_custom().

Sizing a grob in relative units

A circle grob made without a radius takes its size from the space it is given, so width and height set that space.

# Controlling the size of the grob defined in relative units
p + guides(custom = guide_custom(
  circle, title = "My circle",
  width = unit(2, "cm"), height = unit(2, "cm"))
)

Not in Giggly yet: grid grobs, and guide_custom() with its width and height options.

A grob in absolute units

# Size of grobs in absolute units is taken directly without the need to
# set these manually
p + guides(custom = guide_custom(
  title = "My circle",
  grob = grid::circleGrob(r = unit(1, "cm"))
))

Not in Giggly yet: grid grobs and guide_custom().