Powered by AppSignal & Oban Pro

Legend keys: draw_key

notebooks/draw_key.livemd

Legend keys: draw_key

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

About this notebook

This notebook follows the examples on ggplot2's reference page for draw_key_point(), which covers draw_key_abline(), draw_key_rect(), draw_key_polygon(), draw_key_blank(), draw_key_boxplot(), draw_key_crossbar(), draw_key_path(), draw_key_vpath(), draw_key_dotplot(), draw_key_linerange(), draw_key_pointrange(), draw_key_smooth(), draw_key_text(), draw_key_label(), draw_key_vline() and draw_key_timeseries() too, 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.

Each geom has a function that draws its key, the small glyph that stands for the geom in a legend: draw_key_point() for points, draw_key_path() for lines, and so on. A layer can draw its keys with another glyph, named or given as its function, through the layer's key_glyph argument. A draw_key_*() function takes a row of the key's aesthetics, the geom's parameters and the key's size, and returns a grid grob. Giggly has none of this yet: its legends draw each key as the layer's geom draws its own: a point, a bar or rectangle, a line, a filled shape for a ribbon, area or density, a line over its band for a smooth, or the letter "a" for text.

Choosing a layer's key glyph

colour = "savings rate" maps colour to a constant, so the legend has one key, labelled with that text.

p <- ggplot(economics, aes(date, psavert, color = "savings rate"))
# key glyphs can be specified by their name
p + geom_line(key_glyph = "timeseries")

# key glyphs can be specified via their drawing function
p + geom_line(key_glyph = draw_key_rect)

Not in Giggly yet: a layer's key_glyph argument, draw_key_timeseries() and draw_key_rect().