Learn R Programming

legendry

The goal of legendry is to provide additional guide functionality to the ggplot2 ecosystem.

Installation

You can install the development version of legendry from GitHub with:

# install.packages("devtools")
devtools::install_github("teunbrand/legendry")

Example

Let’s first set up a basic plot to experiment with:

library(legendry)
#> Loading required package: ggplot2

base <- ggplot(mpg, aes(displ, hwy, colour = cty)) +
  geom_point() +
  labs(
    x = "Engine displacement",
    y = "Highway miles per gallon",
    col = "City miles\nper gallon"
  ) +
  theme(axis.line = element_line())

The legendry package offers a selection of what it calls ‘complete guides’. These complete guides can just be drop-in replacement of regular guides, which you can specify using ggplot2’s guides() function or using the guide argument in scales. In the example below, we’re using two custom variants of vanilla guides, namely guide_axis_base() and guide_colbar(). These custom variants have additional options that allow a greater degree of customisation:

  • The axis guide has an option for bidirectional ticks.
  • The colourbar automatically recognises out-of-bounds values and displays this with a cap.
base + 
  scale_colour_viridis_c(
    limits = c(NA, 30),
    guide = "colbar"
  ) +
  guides(
    x = guide_axis_base(bidi = TRUE)
  )

Besides complete guides, legendry also has incomplete guides that can be composed. The ggplot2::guide_axis_stack() is an axis composition function that can be used to display multiple guides. Here, we use a ‘primitive’ guide (incomplete building block) to display a range on the axis. By stacking it with a regular axis the primitive guide is completed.

# A partial guide to display a bracket
efficient_bracket <- primitive_bracket(
  # Keys determine what is displayed
  key = key_range_manual(start = 25, end = Inf, name = "Efficient"),
  bracket = "square",
  # We want vertical text
  theme = theme(
    legend.text = element_text(angle = 90, hjust = 0.5),
    axis.text.y.left = element_text(angle = 90, hjust = 0.5)
  )
)

base + guides(y = guide_axis_stack("axis", efficient_bracket))

The legendry package extends this guide composition concept beyond the axes for other types of guides. In the example below we compose a ‘sandwich’: a central guide flanked by two others. Because our bracket is a primitive, it does not matter what aesthetic it displays and we can re-use it for the sandwich. I’ve yet to write the vignette on composition.

base + 
  scale_colour_viridis_c(
    guide = compose_sandwich(
      middle = gizmo_density(), 
      text = "axis_base",
      opposite = efficient_bracket
    )
  )

Code of Conduct

Please note that the legendry project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('legendry')

Monthly Downloads

1,133

Version

0.2.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Teun van den Brand

Last Published

March 4th, 2025

Functions in legendry (0.2.1)

guide_circles

Circle size guide
guide-gizmos

Guide gizmos
guide-composition

Guide composition
guide_axis_nested

Nested axis guide
guide_axis_dendro

Dendrogram guide
guide_colbar

Custom colour bar guide
gizmo_histogram

Guide gizmo: histogram
gizmo_stepcap

Guide gizmo: capped colour steps
guide-primitives

Guide primitives
guide_axis_base

Custom axis guide
guide_legend_base

Custom legend guide
guide_colsteps

Custom colour steps guide
guide_colring

Colour rings and arcs
guide_legend_cross

Cross legend guide
key_standard

Standard keys
key_specialty

Speciality keys
legendry-package

legendry: Extended Legends and Axes for 'ggplot2'
key_segments

Segment keys
primitive_fence

Guide primitive: fence
key_range

Range keys
primitive_labels

Guide primitive: labels
primitive_ticks

Guide primitive: line
primitive_spacer

Guide primitive: spacer
primitive_box

Guide primitives: boxes
primitive_bracket

Guide primitive: brackets
Compose

ggproto objects in legendry
primitive_title

Guide primitive: title
scale_x_dendro

Dendrogram scales
key_group

Group keys
guide_legend_group

Grouped legend
primitive_segments

Guide primitives: segments
theme_guide

Theme wrapper for guides
primitive_line

Guide primitive: line
compose_crux

Compose guides in a cross
cap_options

Cap options
gizmo_grob

Guide gizmo: custom grob
common_parameters

common parameters in legendry
gizmo_barcap

Guide gizmo: capped colour bar
compose_stack

Compose guides as stack
bracket_options

Bracket options
gizmo_density

Guide gizmo: kernel density estimate
compose_ontop

Compose guides on top of one another
compose_sandwich

Compose guides as a sandwich