Powered by AppSignal & Oban Pro

Binned legends: guide_bins

notebooks/guide_bins.livemd

Binned legends: guide_bins

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_bins(), 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_bins() is the legend of a binned scale: a version of guide_legend() that puts its ticks between the keys, at the bins' edges, and draws a small axis to show the binning. It serves any aesthetic but position, though colour and fill default to guide_coloursteps(), and it merges aesthetics mapped the same way into one legend. Giggly has none of it yet: it has no binned scales, and draws a legend for each of alpha, colour and fill and a colour bar for a continuous colour or fill, none of them binned.

The standard look

p <- ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp)) +
  scale_size_binned()

# Standard look
p

Not in Giggly yet: scale_size_binned() and the legend that reads it back, guide_bins().

Removing the legend's axis

# Remove the axis or style it
p + guides(size = guide_bins(
  theme = theme(legend.axis.line = element_blank())
))

Not in Giggly yet: scale_size_binned(), and guide_bins() and its theme option, here to remove the legend's axis line.

Showing the limits

p + guides(size = guide_bins(show.limits = TRUE))

Not in Giggly yet: scale_size_binned(), and guide_bins() with its show.limits option, which labels the ends of the scale too.

Arrows on the legend's axis

R's arrow() describes arrowheads, here 1.5 mm long, at both ends of a line.

my_arrow <- arrow(length = unit(1.5, "mm"), ends = "both")
p + guides(size = guide_bins(
  theme = theme(legend.axis.line = element_line(arrow = my_arrow))
))

Not in Giggly yet: scale_size_binned(), guide_bins() and its theme option, and element_line()'s arrow option, here to put arrowheads on the legend's axis line.

Merged legends

Mapping size and colour to the same column, each through a binned scale, gives one legend for both.

# Guides are merged together if possible
ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp, colour = hp)) +
  scale_size_binned() +
  scale_colour_binned(guide = "bins")

Not in Giggly yet: scale_size_binned(), scale_colour_binned(), and guide_bins(), which merges the two into one legend.