guide_prism_bracket: Axis guide with brackets

View source: R/guide_prism_bracket.R

guide_prism_bracketR Documentation

Axis guide with brackets

Description

This guide turns the axis into brackets drawn around each axis label.

Usage

guide_prism_bracket(
  title = waiver(),
  check.overlap = FALSE,
  angle = NULL,
  n.dodge = 1,
  order = 0,
  position = waiver(),
  width = NULL,
  outside = TRUE
)

Arguments

title

A character string or expression indicating a title of guide. If NULL, the title is not shown. By default (waiver()), the name of the scale object or the name specified in labs() is used for the title.

check.overlap

silently remove overlapping labels, (recursively) prioritizing the first, last, and middle labels.

angle

Compared to setting the angle in theme() / element_text(), this also uses some heuristics to automatically pick the hjust and vjust that you probably want.

n.dodge

The number of rows (for vertical axes) or columns (for horizontal axes) that should be used to render the labels. This is useful for displaying labels that would otherwise overlap.

order

Used to determine the order of the guides (left-to-right, top-to-bottom), if more than one guide must be drawn at the same location.

position

Where this guide should be drawn: one of top, bottom, left, or right.

width

numeric. Controls the width of the bracket. Try values between 0 and 1.

outside

logical. Default is TRUE and brackets point outwards. If FALSE the bracket crossbar is moved so the ticks appear to point inwards towards the plotting area.

Details

The number of brackets can be adjusted using the breaks argument in scale_(x|y)_continuous() or scale_(x|y)_discrete().

Value

Returns a prism_bracket guide class object.

Examples

library(ggplot2)

## base plot
base <- ggplot(mpg, aes(x = as.factor(cyl), y = hwy)) +
  geom_jitter(width = 0.2) +
  theme(axis.line = element_line(colour = "black"))

## use brackets on x axis
# if not specified, the width of the brackets is guessed
base + scale_x_discrete(guide = "prism_bracket")

# you can add brackets using the guide function as well
base + guides(x = "prism_bracket")

## works with coord_flip
base + scale_x_discrete(guide = "prism_bracket") +
  coord_flip()

## adjust bracket width
base + scale_x_discrete(guide = guide_prism_bracket(width = 0.12))

## make brackets point inward
base + scale_x_discrete(guide = guide_prism_bracket(width = 0.12, outside = FALSE))

## change colour with the usual axis.line, axis.ticks, axis.text elements
base + scale_x_discrete(guide = guide_prism_bracket(width = 0.12, outside = FALSE)) +
  theme(axis.line.x = element_line(colour = "red"),
        axis.ticks.x = element_line(colour = "blue"),
        axis.text.x = element_text(colour = "green"))

ggprism documentation built on Nov. 4, 2022, 5:08 p.m.