Title: | Extended Legends and Axes for 'ggplot2' |
---|---|
Description: | A 'ggplot2' extension that focusses on expanding the plotter's arsenal of guides. Guides in 'ggplot2' include axes and legends. 'legendry' offers new axes and annotation options, as well as new legends and colour displays. |
Authors: | Teun van den Brand [aut, cre, cph] |
Maintainer: | Teun van den Brand <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2024-11-02 05:24:44 UTC |
Source: | https://github.com/teunbrand/legendry |
These functions construct various sorts of brackets. They construct a matrix
that can be supplied as the bracket
argument in primitive_bracket()
.
bracket_line() bracket_square() bracket_chevron() bracket_round(angle = 180, n = 100) bracket_sigmoid(curvature = 10, n = 100) bracket_atan(curvature = 5, n = 100) bracket_curvy(angle = 225, n = 100)
bracket_line() bracket_square() bracket_chevron() bracket_round(angle = 180, n = 100) bracket_sigmoid(curvature = 10, n = 100) bracket_atan(curvature = 5, n = 100) bracket_curvy(angle = 225, n = 100)
angle |
A |
n |
An |
curvature |
A |
When designing custom bracket shapes, the expectation is both columns are are a number between 0 and 1. The first column follows the direction of the guide whereas the second column is orthogonal to that direction.
A <matrix[n, 2]>
with coordinates for points on the brackets.
bracket_line()
: A simple line as bracket. Has n = 2
points.
bracket_square()
: A square bracket. Has n = 4
points.
bracket_chevron()
: A chevron (V-shape) that makes a bracket. Has
n = 3
points.
bracket_round()
: One circular arc that makes a bracket.
bracket_sigmoid()
: Two sigmoid curves stacked on top of one another
to form a bracket.
bracket_atan()
: Two arctangent curves stacked on top of one
another to form a bracket.
bracket_curvy()
: Four circular arcs that make a bracket.
plot(bracket_sigmoid(), type = 'l')
plot(bracket_sigmoid(), type = 'l')
These functions construct various sorts of caps. They construct a matrix
that can be supplied as the shape
argument in gizmo_barcap()
.
cap_triangle() cap_round(n = 100) cap_arch(n = 100) cap_ogee(n = 100) cap_none()
cap_triangle() cap_round(n = 100) cap_arch(n = 100) cap_ogee(n = 100) cap_none()
n |
An |
When designing custom cap shapes, the expectation is that the first point
starts at the (0, 0)
coordinate and the last point ends at the (0, 1)
coordinate. The first column follows the orthogonal direction of the bar
whereas the second column follows the direction of the bar.
A <matrix[n, 2]>
with coordinates for points on the brackets.
cap_triangle()
: An equilateral triangle with n = 3
points.
cap_round()
: A semicircle.
cap_arch()
: Two circular arcs forming an equilateral Gothic arch.
cap_ogee()
: Four circular arcs forming an 'ogee' arch.
cap_none()
: No cap.
plot(cap_arch(), type = 'l')
plot(cap_arch(), type = 'l')
This guide composition has a central guide optionally surrounded by other guides on all four sides.
compose_crux( key = NULL, centre = "none", left = "none", right = "none", top = "none", bottom = "none", args = list(), complete = FALSE, theme = NULL, theme_defaults = list(), reverse = FALSE, order = 0, title = waiver(), position = waiver(), available_aes = NULL )
compose_crux( key = NULL, centre = "none", left = "none", right = "none", top = "none", bottom = "none", args = list(), complete = FALSE, theme = NULL, theme_defaults = list(), reverse = FALSE, order = 0, title = waiver(), position = waiver(), available_aes = NULL )
key |
A standard key specification. The key is shared
among all guides that have |
centre , left , right , top , bottom
|
Guides to use in composition per position. Each guide can be specified as one of the following:
|
args |
A |
complete |
A |
theme |
A |
theme_defaults |
A |
reverse |
A |
order |
A positive |
title |
A |
position |
Where this guide should be drawn: one of |
available_aes |
A |
A <ComposeCrux>
guide object.
Other composition:
compose_ontop()
,
compose_sandwich()
,
compose_stack()
,
guide-composition
# Roughly recreating a colour bar with extra text on top and bottom crux <- compose_crux( centre = gizmo_barcap(), left = "axis_base", right = "axis_base", top = primitive_title("A lot"), bottom = primitive_title("A little") ) ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = cty)) + guides(colour = crux)
# Roughly recreating a colour bar with extra text on top and bottom crux <- compose_crux( centre = gizmo_barcap(), left = "axis_base", right = "axis_base", top = primitive_title("A lot"), bottom = primitive_title("A little") ) ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = cty)) + guides(colour = crux)
This guide can place place other guides on top of one another.
compose_ontop( ..., args = list(), key = NULL, title = waiver(), angle = waiver(), theme = NULL, order = 0, position = waiver(), available_aes = NULL )
compose_ontop( ..., args = list(), key = NULL, title = waiver(), angle = waiver(), theme = NULL, order = 0, position = waiver(), available_aes = NULL )
... |
Guides to stack in composition. Each guide can be specified as one of the following:
|
args |
A |
key |
A standard key specification. The key is shared
among all guides that have |
title |
A |
angle |
A specification for the text angle. Compared to setting the
|
theme |
A |
order |
A positive |
position |
A |
available_aes |
A |
A <ComposeOntop>
composite guide object.
Other composition:
compose_crux()
,
compose_sandwich()
,
compose_stack()
,
guide-composition
# Using the ontop composition to get two types of ticks with different # lengths ggplot(mpg, aes(displ, hwy)) + geom_point() + guides(x = compose_ontop( guide_axis_base( key_manual(c(2, 4, 6)), theme = theme( axis.ticks = element_line(colour = "limegreen"), axis.ticks.length = unit(11, "pt") ) ), guide_axis_base( key_manual(c(3, 5, 7)), theme = theme( axis.ticks = element_line(colour = "tomato"), axis.ticks.length = unit(5.5, "pt") ) ) ))
# Using the ontop composition to get two types of ticks with different # lengths ggplot(mpg, aes(displ, hwy)) + geom_point() + guides(x = compose_ontop( guide_axis_base( key_manual(c(2, 4, 6)), theme = theme( axis.ticks = element_line(colour = "limegreen"), axis.ticks.length = unit(11, "pt") ) ), guide_axis_base( key_manual(c(3, 5, 7)), theme = theme( axis.ticks = element_line(colour = "tomato"), axis.ticks.length = unit(5.5, "pt") ) ) ))
This guide composition has a middle guide flanked by two parallel guides.
compose_sandwich( key = key_auto(), middle = gizmo_barcap(), text = "none", opposite = "none", args = list(), complete = TRUE, theme = NULL, theme_defaults = list(), reverse = FALSE, order = 0, title = waiver(), position = waiver(), available_aes = NULL )
compose_sandwich( key = key_auto(), middle = gizmo_barcap(), text = "none", opposite = "none", args = list(), complete = TRUE, theme = NULL, theme_defaults = list(), reverse = FALSE, order = 0, title = waiver(), position = waiver(), available_aes = NULL )
key |
A standard key specification. The key is shared
among all guides that have |
middle |
Guide to use as the middle guide. Each guide can be specified as one of the following:
|
text , opposite
|
Guides to use at the |
args |
A |
complete |
A |
theme |
A |
theme_defaults |
A |
reverse |
A |
order |
A positive |
title |
A |
position |
Where this guide should be drawn: one of |
available_aes |
A |
The sandwich composition is effectively the same as a crux composition lacking two opposing arms.
A <ComposeSandwich>
guide object.
Other composition:
compose_crux()
,
compose_ontop()
,
compose_stack()
,
guide-composition
# A standard plot with a sandwich guide ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = cty)) + guides(colour = compose_sandwich( middle = "colourbar", text = "axis_base", opposite = primitive_bracket(key = key_range_manual( start = c(10, 20), end = c(25, 30), name = c("A", "B") )) ))
# A standard plot with a sandwich guide ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = cty)) + guides(colour = compose_sandwich( middle = "colourbar", text = "axis_base", opposite = primitive_bracket(key = key_range_manual( start = c(10, 20), end = c(25, 30), name = c("A", "B") )) ))
This guide can stack other guides.
compose_stack( ..., args = list(), key = NULL, title = waiver(), side.titles = waiver(), angle = waiver(), theme = NULL, order = 0, drop = NULL, position = waiver(), available_aes = NULL )
compose_stack( ..., args = list(), key = NULL, title = waiver(), side.titles = waiver(), angle = waiver(), theme = NULL, order = 0, drop = NULL, position = waiver(), available_aes = NULL )
... |
Guides to stack in composition. Each guide can be specified as one of the following:
|
args |
A |
key |
A standard key specification. The key is shared
among all guides that have |
title |
A |
side.titles |
A |
angle |
A specification for the text angle. Compared to setting the
|
theme |
A |
order |
A positive |
drop |
An |
position |
A |
available_aes |
A |
A <ComposeStack>
guide object.
Other composition:
compose_crux()
,
compose_ontop()
,
compose_sandwich()
,
guide-composition
ggplot() + geom_function(fun = dnorm, xlim = c(-3, 3)) + guides(x = compose_stack( "axis", "axis", side.titles = c("first", "second") )) + # Add margin to make room for side titles theme(plot.margin = margin(5.5, 5.5, 5.5, 11))
ggplot() + geom_function(fun = dnorm, xlim = c(-3, 3)) + guides(x = compose_stack( "axis", "axis", side.titles = c("first", "second") )) + # Add margin to make room for side titles theme(plot.margin = margin(5.5, 5.5, 5.5, 11))
This guide displays a colour bar with optional caps at either ends of the bar.
gizmo_barcap( key = "sequence", shape = "triangle", size = NULL, show = NA, alpha = NA, oob = "keep", theme = NULL, position = waiver(), direction = NULL )
gizmo_barcap( key = "sequence", shape = "triangle", size = NULL, show = NA, alpha = NA, oob = "keep", theme = NULL, position = waiver(), direction = NULL )
key |
A sequence key specification. Defaults to
|
shape |
A cap specification by providing one of the following:
|
size |
A |
show |
A |
alpha |
A |
oob |
An out-of-bounds handling function that affects the cap colour. Can be one of the following:
|
theme |
A |
position |
A |
direction |
A |
A <GizmoBarcap>
object.
Other gizmos:
gizmo_density()
,
gizmo_grob()
,
gizmo_histogram()
,
gizmo_stepcap()
# A standard plot p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() # Just a bar p + scale_colour_viridis_c(guide = gizmo_barcap()) # Caps show up when there is data outside the limits p + scale_colour_viridis_c( limits = c(10, 30), guide = gizmo_barcap() ) # The scale's out-of-bounds handler determines cap colour p + scale_colour_viridis_c( limits = c(10, 30), oob = scales::oob_squish, guide = gizmo_barcap() ) # Customising display of the guide p + scale_colour_viridis_c( oob = scales::oob_squish, guide = gizmo_barcap( shape = "arch", show = c(FALSE, TRUE), size = unit(2, "cm"), theme = theme(legend.key.height = unit(4, "cm")) ) ) + theme( legend.frame = element_rect(colour = "black"), legend.key.width = unit(0.5, "cm") )
# A standard plot p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() # Just a bar p + scale_colour_viridis_c(guide = gizmo_barcap()) # Caps show up when there is data outside the limits p + scale_colour_viridis_c( limits = c(10, 30), guide = gizmo_barcap() ) # The scale's out-of-bounds handler determines cap colour p + scale_colour_viridis_c( limits = c(10, 30), oob = scales::oob_squish, guide = gizmo_barcap() ) # Customising display of the guide p + scale_colour_viridis_c( oob = scales::oob_squish, guide = gizmo_barcap( shape = "arch", show = c(FALSE, TRUE), size = unit(2, "cm"), theme = theme(legend.key.height = unit(4, "cm")) ) ) + theme( legend.frame = element_rect(colour = "black"), legend.key.width = unit(0.5, "cm") )
This guide displays a kernel density estimate (KDE) of the aesthetic. If the
aesthetic is colour
or fill
, the shape will reflect this.
gizmo_density( key = "sequence", density = NULL, density.args = list(), density.fun = stats::density, just = 0.5, oob = "keep", alpha = NA, theme = NULL, position = waiver(), direction = NULL )
gizmo_density( key = "sequence", density = NULL, density.args = list(), density.fun = stats::density, just = 0.5, oob = "keep", alpha = NA, theme = NULL, position = waiver(), direction = NULL )
key |
A sequence key or binned key specification. |
density |
One of the following:
|
density.args |
A |
density.fun |
A |
just |
A |
oob |
An out-of-bounds handling function that affects the cap colour. Can be one of the following:
|
alpha |
A |
theme |
A |
position |
A |
direction |
A |
Non-finite values such as NA
and NaN
are ignored while infinite values
such as -Inf
and Inf
are squished to the limits.
A <GizmoDensity>
object.
Other gizmos:
gizmo_barcap()
,
gizmo_grob()
,
gizmo_histogram()
,
gizmo_stepcap()
# A standard plot p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + scale_colour_viridis_c() # Density from plot data p + guides(colour = gizmo_density()) # Using bins instead of gradient p + guides(colour = gizmo_density("bins")) # Providing custom values to compute density of p + guides(colour = gizmo_density(density = runif(1000, min = 5, max = 35))) # Providing a precomputed density p + guides(colour = gizmo_density(density = density(mpg$cty, adjust = 0.5))) # Alternatively, parameters may be passed through density.args p + guides(colour = gizmo_density(density.args = list(adjust = 0.5)))
# A standard plot p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + scale_colour_viridis_c() # Density from plot data p + guides(colour = gizmo_density()) # Using bins instead of gradient p + guides(colour = gizmo_density("bins")) # Providing custom values to compute density of p + guides(colour = gizmo_density(density = runif(1000, min = 5, max = 35))) # Providing a precomputed density p + guides(colour = gizmo_density(density = density(mpg$cty, adjust = 0.5))) # Alternatively, parameters may be passed through density.args p + guides(colour = gizmo_density(density.args = list(adjust = 0.5)))
This guide displays a user-provided grob.
gizmo_grob( grob, width = grobWidth(grob), height = grobHeight(grob), hjust = 0.5, vjust = 0.5, position = waiver() )
gizmo_grob( grob, width = grobWidth(grob), height = grobHeight(grob), hjust = 0.5, vjust = 0.5, position = waiver() )
grob |
A |
width , height
|
A [ |
hjust , vjust
|
A |
position |
Where this guide should be drawn: one of |
A <GizmoGrob>
object.
Other gizmos:
gizmo_barcap()
,
gizmo_density()
,
gizmo_histogram()
,
gizmo_stepcap()
circle <- grid::circleGrob() # A standard plot with grob gizmos ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + guides( x.sec = gizmo_grob( circle, hjust = 0.75, width = unit(2, "cm"), height = unit(2, "cm") ), colour = gizmo_grob( circle, width = unit(1, "cm"), height = unit(1, "cm") ) )
circle <- grid::circleGrob() # A standard plot with grob gizmos ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + guides( x.sec = gizmo_grob( circle, hjust = 0.75, width = unit(2, "cm"), height = unit(2, "cm") ), colour = gizmo_grob( circle, width = unit(1, "cm"), height = unit(1, "cm") ) )
This guide displays a histogram of the aesthetic. If the aesthetic is
colour
or fill
, the shape will reflect this.
gizmo_histogram( key = "sequence", hist = NULL, hist.args = list(), hist.fun = graphics::hist, just = 1, oob = oob_keep, alpha = NA, theme = NULL, position = waiver(), direction = NULL )
gizmo_histogram( key = "sequence", hist = NULL, hist.args = list(), hist.fun = graphics::hist, just = 1, oob = oob_keep, alpha = NA, theme = NULL, position = waiver(), direction = NULL )
key |
A sequence key or binned key specification. |
hist |
One of the following:
|
hist.args |
A |
hist.fun |
A |
just |
A |
oob |
An out-of-bounds handling function that affects the cap colour. Can be one of the following:
|
alpha |
A |
theme |
A |
position |
A |
direction |
A |
Non-finite values such as NA
and NaN
are ignored while infinite values
such as -Inf
and Inf
are squished to the limits.
A <GizmoHistogram>
object.
Other gizmos:
gizmo_barcap()
,
gizmo_density()
,
gizmo_grob()
,
gizmo_stepcap()
# A standard plot p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + scale_colour_viridis_c() # Histogram from plot data p + guides(colour = gizmo_histogram()) # Using bins instead of gradient p + guides(colour = gizmo_histogram("bins")) # Providing custom values to compute histogram p + guides(colour = gizmo_histogram(hist = runif(1000, min = 5, max = 35))) # Providing precomputed histogram p + guides(colour = gizmo_histogram(hist = hist(mpg$cty, breaks = 10))) # Alternatively, parameters may be passed through hist.args p + guides(colour = gizmo_histogram(hist.arg = list(breaks = 10)))
# A standard plot p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + scale_colour_viridis_c() # Histogram from plot data p + guides(colour = gizmo_histogram()) # Using bins instead of gradient p + guides(colour = gizmo_histogram("bins")) # Providing custom values to compute histogram p + guides(colour = gizmo_histogram(hist = runif(1000, min = 5, max = 35))) # Providing precomputed histogram p + guides(colour = gizmo_histogram(hist = hist(mpg$cty, breaks = 10))) # Alternatively, parameters may be passed through hist.args p + guides(colour = gizmo_histogram(hist.arg = list(breaks = 10)))
This guide displays a binned variant of the colour bar with optional caps at either ends of the bar.
gizmo_stepcap( key = "bins", shape = "triangle", size = NULL, show = NA, alpha = NA, oob = "keep", theme = NULL, position = waiver(), direction = NULL )
gizmo_stepcap( key = "bins", shape = "triangle", size = NULL, show = NA, alpha = NA, oob = "keep", theme = NULL, position = waiver(), direction = NULL )
key |
A bins key specificiation. Defaults to
|
shape |
A cap specification by providing one of the following:
|
size |
A |
show |
A |
alpha |
A |
oob |
An out-of-bounds handling function that affects the cap colour. Can be one of the following:
|
theme |
A |
position |
A |
direction |
A |
A GizmoStepcap
object.
Other gizmos:
gizmo_barcap()
,
gizmo_density()
,
gizmo_grob()
,
gizmo_histogram()
# A standard plot p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() # Just some recangles p + scale_colour_viridis_c(guide = gizmo_stepcap()) # Caps show up when there is data outside the limits p + scale_colour_viridis_c( limits = c(10, 30), guide = gizmo_stepcap() ) # The scale's out-of-bounds handler determines cap colour p + scale_colour_viridis_c( limits = c(10, 30), oob = scales::oob_squish, guide = gizmo_stepcap() ) # Customising the display of the guide p + scale_colour_viridis_c( oob = scales::oob_squish, guide = gizmo_stepcap( shape = "round", show = c(FALSE, TRUE), size = unit(1, "cm"), theme = theme(legend.key.height = unit(4, "cm")) ) ) + theme( legend.frame = element_rect(colour = "black"), legend.key.width = unit(0.5, "cm") )
# A standard plot p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() # Just some recangles p + scale_colour_viridis_c(guide = gizmo_stepcap()) # Caps show up when there is data outside the limits p + scale_colour_viridis_c( limits = c(10, 30), guide = gizmo_stepcap() ) # The scale's out-of-bounds handler determines cap colour p + scale_colour_viridis_c( limits = c(10, 30), oob = scales::oob_squish, guide = gizmo_stepcap() ) # Customising the display of the guide p + scale_colour_viridis_c( oob = scales::oob_squish, guide = gizmo_stepcap( shape = "round", show = c(FALSE, TRUE), size = unit(1, "cm"), theme = theme(legend.key.height = unit(4, "cm")) ) ) + theme( legend.frame = element_rect(colour = "black"), legend.key.width = unit(0.5, "cm") )
This axis guide is a visual representation of position scales and can
represent the x
, y
, theta
and r
aesthetics. It differs from
guide_axis()
in that it can accept custom keys
and is can act as an axis for coord_radial()
like
guide_axis_theta()
.
guide_axis_base( key = NULL, title = waiver(), theme = NULL, n.dodge = 1, check.overlap = FALSE, angle = waiver(), cap = "none", bidi = FALSE, order = 0, position = waiver() )
guide_axis_base( key = NULL, title = waiver(), theme = NULL, n.dodge = 1, check.overlap = FALSE, angle = waiver(), cap = "none", bidi = FALSE, order = 0, position = waiver() )
key |
A standard key specification. Defaults to
|
title |
A |
theme |
A |
n.dodge |
An positive |
check.overlap |
A |
angle |
A specification for the text angle. Compared to setting the
|
cap |
A method to cap the axes. One of the following:
|
bidi |
A |
order |
A positive |
position |
A |
Under the hood, this guide is a stack composition of a line, ticks and labels primitives.
To set minor ticks, use key = "minor"
, or use the type
argument in
key_manual()
or key_map()
.
To use this as a logarithmic axis, set key = "log"
.
A <Guide>
object.
Other standalone guides:
guide_axis_nested()
,
guide_colbar()
,
guide_colring()
,
guide_colsteps()
,
guide_legend_base()
,
guide_legend_cross()
,
guide_legend_group()
# A standard plot with custom keys p <- ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_x_continuous( guide = guide_axis_base(key = key_minor()) ) + scale_y_continuous( guide = guide_axis_base(key = key_manual(c(20, 25, 30, 40))) ) p # Is translated to theta axis without fuss p + coord_radial() # To use as logarithmic axis: ggplot(msleep, aes(bodywt, brainwt)) + geom_point(na.rm = TRUE) + scale_x_continuous( transform = "log10", guide = guide_axis_base("log") )
# A standard plot with custom keys p <- ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_x_continuous( guide = guide_axis_base(key = key_minor()) ) + scale_y_continuous( guide = guide_axis_base(key = key_manual(c(20, 25, 30, 40))) ) p # Is translated to theta axis without fuss p + coord_radial() # To use as logarithmic axis: ggplot(msleep, aes(bodywt, brainwt)) + geom_point(na.rm = TRUE) + scale_x_continuous( transform = "log10", guide = guide_axis_base("log") )
This axis guide gives extra range annotations to position scales. It can be used to infer nesting structure from labels or annotate ranges.
guide_axis_nested( key = "range_auto", regular_key = "auto", type = "bracket", title = waiver(), theme = NULL, angle = waiver(), cap = "none", bidi = FALSE, oob = "squish", drop_zero = TRUE, pad_discrete = NULL, levels_text = NULL, ..., order = 0, position = waiver() )
guide_axis_nested( key = "range_auto", regular_key = "auto", type = "bracket", title = waiver(), theme = NULL, angle = waiver(), cap = "none", bidi = FALSE, oob = "squish", drop_zero = TRUE, pad_discrete = NULL, levels_text = NULL, ..., order = 0, position = waiver() )
key |
A range key specification. If not
|
regular_key |
A standard key specification for the appearance of regular tick marks. |
type |
Appearance of ranges, either |
title |
A |
theme |
A |
angle |
A specification for the text angle. Compared to setting the
|
cap |
A method to cap the axes. One of the following:
|
bidi |
A |
oob |
A method for dealing with out-of-bounds (oob) ranges. Can be one
of |
drop_zero |
A |
pad_discrete |
A |
levels_text |
A list of |
... |
Arguments passed on to |
order |
A positive |
position |
A |
Under the hood, this guide is a stack composition of a line, ticks, optionally labels and either bracket, box or fence primitives.
By default, the key = "range_auto"
will incorporate the 0th
level labels inferred from the scale's labels. These labels will look like
regular labels.
To offer other keys the opportunity to display ranges alongside
regular-looking labels, the regular_key
argument can be used to setup a
separate key for display in between the ticks and ranges.
A <Guide>
object.
Other standalone guides:
guide_axis_base()
,
guide_colbar()
,
guide_colring()
,
guide_colsteps()
,
guide_legend_base()
,
guide_legend_cross()
,
guide_legend_group()
# A plot with nested categories on the x-axis p <- ggplot(mpg, aes(interaction(drv, cyl), hwy)) + geom_boxplot() p + guides(x = "axis_nested") # Apply styling to brackets p + guides(x = "axis_nested") + theme_guide(bracket = element_line("red", linewidth = 1)) # Don't drop nesting indicators that have 0-width p + guides(x = guide_axis_nested(drop_zero = FALSE)) # Change additional padding for discrete categories p + guides(x = guide_axis_nested(pad_discrete = 0)) # Change bracket type p + guides(x = guide_axis_nested(bracket = "curvy")) # Use boxes instead of brackets + styling of boxes p + guides(x = guide_axis_nested(type = "box")) + theme_guide(box = element_rect("limegreen", "forestgreen")) # Using fences instead of brackets + styling of fences p + guides(x = guide_axis_nested(type = "fence", rail = "inner")) + theme_guide( fence.post = element_line("tomato"), fence.rail = element_line("dodgerblue") ) # Use as annotation of a typical axis # `regular_key` controls display of typical axis ggplot(mpg, aes(displ, hwy)) + geom_point() + guides(x = guide_axis_nested( key = key_range_manual(start = 2:3, end = 5:6, name = c("First", "Second")), regular_key = key_manual(c(2, 2.5, 3, 5, 7)) ))
# A plot with nested categories on the x-axis p <- ggplot(mpg, aes(interaction(drv, cyl), hwy)) + geom_boxplot() p + guides(x = "axis_nested") # Apply styling to brackets p + guides(x = "axis_nested") + theme_guide(bracket = element_line("red", linewidth = 1)) # Don't drop nesting indicators that have 0-width p + guides(x = guide_axis_nested(drop_zero = FALSE)) # Change additional padding for discrete categories p + guides(x = guide_axis_nested(pad_discrete = 0)) # Change bracket type p + guides(x = guide_axis_nested(bracket = "curvy")) # Use boxes instead of brackets + styling of boxes p + guides(x = guide_axis_nested(type = "box")) + theme_guide(box = element_rect("limegreen", "forestgreen")) # Using fences instead of brackets + styling of fences p + guides(x = guide_axis_nested(type = "fence", rail = "inner")) + theme_guide( fence.post = element_line("tomato"), fence.rail = element_line("dodgerblue") ) # Use as annotation of a typical axis # `regular_key` controls display of typical axis ggplot(mpg, aes(displ, hwy)) + geom_point() + guides(x = guide_axis_nested( key = key_range_manual(start = 2:3, end = 5:6, name = c("First", "Second")), regular_key = key_manual(c(2, 2.5, 3, 5, 7)) ))
Similar to guide_colourbar()
, this guide
displays continuous colour
or fill
aesthetics. It has additional options
to display caps at the end of the bar, depending on out-of-bounds values.
guide_colbar( title = waiver(), key = "auto", first_guide = "axis_base", second_guide = first_guide, shape = "triangle", size = NULL, show = NA, nbin = 15, alpha = NA, reverse = FALSE, oob = scales::oob_keep, theme = NULL, vanilla = TRUE, position = waiver(), available_aes = c("colour", "fill") )
guide_colbar( title = waiver(), key = "auto", first_guide = "axis_base", second_guide = first_guide, shape = "triangle", size = NULL, show = NA, nbin = 15, alpha = NA, reverse = FALSE, oob = scales::oob_keep, theme = NULL, vanilla = TRUE, position = waiver(), available_aes = c("colour", "fill") )
title |
A |
key |
A sequence key specification. Defaults to
|
first_guide , second_guide
|
Guides to flank the colour bar. Each guide can be specified using one of the following:
The |
shape |
A cap specification by providing one of the following:
|
size |
A |
show |
A |
nbin |
A positive |
alpha |
A |
reverse |
A |
oob |
An out-of-bounds handling function that affects the cap colour. Can be one of the following:
|
theme |
A |
vanilla |
A |
position |
A |
available_aes |
A |
As colours are always rendered as gradients, it is important to use a
graphics device that can render these. This can be checked by using
check_device("gradients")
.
A <Guide>
object
Other standalone guides:
guide_axis_base()
,
guide_axis_nested()
,
guide_colring()
,
guide_colsteps()
,
guide_legend_base()
,
guide_legend_cross()
,
guide_legend_group()
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = cty)) # The colourbar shows caps when values are out-of-bounds (oob) p + scale_colour_viridis_c( limits = c(10, NA), guide = "colbar" ) # It also shows how oob values are handled p + scale_colour_viridis_c( limits = c(10, NA), oob = scales::oob_squish, guide = "colbar" ) # Adjusting the type of cap p + scale_colour_viridis_c( limits = c(10, 30), oob = scales::oob_squish, guide = guide_colbar(shape = "round") ) # One-sided ticks p + scale_colour_viridis_c( guide = guide_colbar(second_guide = "none") ) # Colour bar with minor breaks p + scale_colour_viridis_c( minor_breaks = scales::breaks_width(1), guide = guide_colbar(key = "minor") ) # Using log ticks on a colourbar ggplot(msleep, aes(sleep_total, sleep_rem)) + geom_point(aes(colour = bodywt), na.rm = TRUE) + scale_colour_viridis_c( transform = "log10", guide = guide_colbar(key = "log") )
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = cty)) # The colourbar shows caps when values are out-of-bounds (oob) p + scale_colour_viridis_c( limits = c(10, NA), guide = "colbar" ) # It also shows how oob values are handled p + scale_colour_viridis_c( limits = c(10, NA), oob = scales::oob_squish, guide = "colbar" ) # Adjusting the type of cap p + scale_colour_viridis_c( limits = c(10, 30), oob = scales::oob_squish, guide = guide_colbar(shape = "round") ) # One-sided ticks p + scale_colour_viridis_c( guide = guide_colbar(second_guide = "none") ) # Colour bar with minor breaks p + scale_colour_viridis_c( minor_breaks = scales::breaks_width(1), guide = guide_colbar(key = "minor") ) # Using log ticks on a colourbar ggplot(msleep, aes(sleep_total, sleep_rem)) + geom_point(aes(colour = bodywt), na.rm = TRUE) + scale_colour_viridis_c( transform = "log10", guide = guide_colbar(key = "log") )
Similar to guide_colourbar()
, this guide
displays continuous colour
or fill
aesthetics. Instead of a bar, the
gradient in shown in a ring or arc, which can be convenient for cyclical
palettes such as some provided in the scico package.
guide_colring( title = waiver(), key = "auto", start = 0, end = NULL, outer_guide = "axis_base", inner_guide = "axis_base", nbin = 300, reverse = FALSE, show_labels = "outer", theme = NULL, vanilla = TRUE, position = waiver(), available_aes = c("colour", "fill"), ... )
guide_colring( title = waiver(), key = "auto", start = 0, end = NULL, outer_guide = "axis_base", inner_guide = "axis_base", nbin = 300, reverse = FALSE, show_labels = "outer", theme = NULL, vanilla = TRUE, position = waiver(), available_aes = c("colour", "fill"), ... )
title |
A |
key |
A standard key specification. Defaults to
|
start , end
|
A |
outer_guide , inner_guide
|
Guides to display on the outside and inside of the colour ring. Each guide can be specified using one of the following:
|
nbin |
A positive |
reverse |
A |
show_labels |
A |
theme |
A |
vanilla |
A |
position |
A |
available_aes |
A |
... |
Arguments forwarded to the |
A <Guide>
object.
Other standalone guides:
guide_axis_base()
,
guide_axis_nested()
,
guide_colbar()
,
guide_colsteps()
,
guide_legend_base()
,
guide_legend_cross()
,
guide_legend_group()
# Rings works best with a cyclical palette my_pal <- c("black", "tomato", "white", "dodgerblue", "black") p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + scale_colour_gradientn(colours = my_pal) # Standard colour ring p + guides(colour = "colring") # As an arc p + guides(colour = guide_colring( start = 1.25 * pi, end = 2.75 * pi )) # Removing the inner tick marks p + guides(colour = guide_colring(inner_guide = "none")) # Include labels on the inner axis p + guides(colour = guide_colring(show_labels = "both")) # Passing an argument to inner/outer guides p + guides(colour = guide_colring(angle = 0))
# Rings works best with a cyclical palette my_pal <- c("black", "tomato", "white", "dodgerblue", "black") p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + scale_colour_gradientn(colours = my_pal) # Standard colour ring p + guides(colour = "colring") # As an arc p + guides(colour = guide_colring( start = 1.25 * pi, end = 2.75 * pi )) # Removing the inner tick marks p + guides(colour = guide_colring(inner_guide = "none")) # Include labels on the inner axis p + guides(colour = guide_colring(show_labels = "both")) # Passing an argument to inner/outer guides p + guides(colour = guide_colring(angle = 0))
Similar to guide_coloursteps()
, this guide
displays continuous colour
or fill
aesthetics. It has additional options
to display caps at the end of the bar, depending on out-of-bounds values.
guide_colsteps( title = waiver(), key = "bins", first_guide = "axis_base", second_guide = "axis_base", shape = "triangle", size = NULL, show = NA, alpha = NA, reverse = FALSE, oob = scales::oob_keep, theme = NULL, position = waiver(), vanilla = TRUE, available_aes = c("colour", "fill") )
guide_colsteps( title = waiver(), key = "bins", first_guide = "axis_base", second_guide = "axis_base", shape = "triangle", size = NULL, show = NA, alpha = NA, reverse = FALSE, oob = scales::oob_keep, theme = NULL, position = waiver(), vanilla = TRUE, available_aes = c("colour", "fill") )
title |
A |
key |
A bins key specificiation. Defaults to
|
first_guide , second_guide
|
Guides to flank the colour steps. Each guide can be specified using one of the following:
The |
shape |
A cap specification by providing one of the following:
|
size |
A |
show |
A |
alpha |
A |
reverse |
A |
oob |
An out-of-bounds handling function that affects the cap colour. Can be one of the following:
|
theme |
A |
position |
A |
vanilla |
A |
available_aes |
A |
As steps are rendered as clipped rectangles, it is important to use a
graphics device that can render clipped paths. This can be checked by using
check_device("clippingPaths")
.
A <Guide>
object
Other standalone guides:
guide_axis_base()
,
guide_axis_nested()
,
guide_colbar()
,
guide_colring()
,
guide_legend_base()
,
guide_legend_cross()
,
guide_legend_group()
p <- ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = cty)) # The colour steps show caps when values are out-of-bounds p + scale_colour_viridis_b( limits = c(10, NA), guide = "colsteps" ) # It also shows how oob values are handled p + scale_colour_viridis_b( limits = c(10, 30), oob = scales::oob_censor, guide = "colsteps" ) # Adjusting the type of cap p + scale_colour_viridis_b( limits = c(10, 30), guide = guide_colsteps(shape = "round") ) # The default is to use the breaks as-is p + scale_colour_viridis_b( limits = c(10, 30), breaks = c(10, 20, 25), guide = "colsteps" ) # But the display can be set to use evenly spaced steps p + scale_colour_viridis_b( limits = c(10, 30), breaks = c(10, 20, 25), guide = guide_colsteps(key = key_bins(even.steps = TRUE)) ) # Using tick marks by swapping side guides p + scale_colour_viridis_b( guide = guide_colsteps( first_guide = "axis_base", second_guide = "axis_base" ) )
p <- ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = cty)) # The colour steps show caps when values are out-of-bounds p + scale_colour_viridis_b( limits = c(10, NA), guide = "colsteps" ) # It also shows how oob values are handled p + scale_colour_viridis_b( limits = c(10, 30), oob = scales::oob_censor, guide = "colsteps" ) # Adjusting the type of cap p + scale_colour_viridis_b( limits = c(10, 30), guide = guide_colsteps(shape = "round") ) # The default is to use the breaks as-is p + scale_colour_viridis_b( limits = c(10, 30), breaks = c(10, 20, 25), guide = "colsteps" ) # But the display can be set to use evenly spaced steps p + scale_colour_viridis_b( limits = c(10, 30), breaks = c(10, 20, 25), guide = guide_colsteps(key = key_bins(even.steps = TRUE)) ) # Using tick marks by swapping side guides p + scale_colour_viridis_b( guide = guide_colsteps( first_guide = "axis_base", second_guide = "axis_base" ) )
This legend closely mirrors ggplot2::guide_legend()
, but has two
adjustments. First, guide_legend_base()
supports a design
argument
for a more flexible layout. Secondly, the legend.spacing.y
theme element
is observed verbatim instead of overruled.
guide_legend_base( key = NULL, title = waiver(), theme = NULL, design = NULL, nrow = NULL, ncol = NULL, reverse = FALSE, override.aes = list(), position = NULL, direction = NULL, order = 0 )
guide_legend_base( key = NULL, title = waiver(), theme = NULL, design = NULL, nrow = NULL, ncol = NULL, reverse = FALSE, override.aes = list(), position = NULL, direction = NULL, order = 0 )
key |
A standard key specification. Defaults to
|
title |
A |
theme |
A |
design |
Specification of the legend layout. One of the following:
|
nrow , ncol
|
A positive |
reverse |
A |
override.aes |
A named |
position |
A |
direction |
A |
order |
A positive |
A <GuideLegend>
object.
Other standalone guides:
guide_axis_base()
,
guide_axis_nested()
,
guide_colbar()
,
guide_colring()
,
guide_colsteps()
,
guide_legend_cross()
,
guide_legend_group()
Other legend guides:
guide_legend_cross()
,
guide_legend_group()
# A dummy plot p <- ggplot(data.frame(x = 1:3, type = c("tic", "tac", "toe"))) + aes(x, x, shape = type) + geom_point(na.rm = TRUE) + scale_shape_manual(values = c(1, 4, NA)) # A design string, each character giving a cell value. # Newlines separate rows, white space is ignored. design <- " 123 213 321 " # Alternatively, the same can be specified using a matrix directly # design <- matrix(c(1, 2, 3, 2, 1, 3, 3, 2, 1), 3, 3, byrow = TRUE) p + guides(shape = guide_legend_base(design = design)) # Empty cells can be created using `#` design <- " #2# 1#3 " # Alternatively: # design <- matrix(c(NA, 1, 2, NA, NA, 3), nrow = 2) p + guides(shape = guide_legend_base(design = design))
# A dummy plot p <- ggplot(data.frame(x = 1:3, type = c("tic", "tac", "toe"))) + aes(x, x, shape = type) + geom_point(na.rm = TRUE) + scale_shape_manual(values = c(1, 4, NA)) # A design string, each character giving a cell value. # Newlines separate rows, white space is ignored. design <- " 123 213 321 " # Alternatively, the same can be specified using a matrix directly # design <- matrix(c(1, 2, 3, 2, 1, 3, 3, 2, 1), 3, 3, byrow = TRUE) p + guides(shape = guide_legend_base(design = design)) # Empty cells can be created using `#` design <- " #2# 1#3 " # Alternatively: # design <- matrix(c(NA, 1, 2, NA, NA, 3), nrow = 2) p + guides(shape = guide_legend_base(design = design))
This is a legend type similar to guide_legend()
that displays crosses, or: interactions, between two variables.
guide_legend_cross( key = NULL, title = waiver(), swap = FALSE, col_text = element_text(angle = 90, vjust = 0.5), override.aes = list(), reverse = FALSE, theme = NULL, position = NULL, direction = NULL, order = 0 )
guide_legend_cross( key = NULL, title = waiver(), swap = FALSE, col_text = element_text(angle = 90, vjust = 0.5), override.aes = list(), reverse = FALSE, theme = NULL, position = NULL, direction = NULL, order = 0 )
key |
One of the following key specifications:
|
title |
A |
swap |
A |
col_text |
An |
override.aes |
A named |
reverse |
A |
theme |
A |
position |
A |
direction |
A |
order |
A positive |
A <GuideLegend>
object.
Other standalone guides:
guide_axis_base()
,
guide_axis_nested()
,
guide_colbar()
,
guide_colring()
,
guide_colsteps()
,
guide_legend_base()
,
guide_legend_group()
Other legend guides:
guide_legend_base()
,
guide_legend_group()
# Standard use for single aesthetic. The default is to split labels to # disentangle aesthetics that are already crossed (by e.g. `paste()`) ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = paste(year, drv))) + guides(colour = "legend_cross") # If legends should be merged between identical aesthetics, both need the # same legend type. ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = paste(year, drv), shape = paste(year, drv))) + guides(colour = "legend_cross", shape = "legend_cross") # Crossing two aesthetics requires a shared title and `key = "auto"`. The # easy way to achieve this is to predefine a shared guide. my_guide <- guide_legend_cross(key = "auto", title = "My title") ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = drv, shape = factor(year))) + guides(colour = my_guide, shape = my_guide) # You can cross more than 2 aesthetics but not more than 2 unique aesthetics. ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = drv, shape = factor(year), size = factor(drv))) + scale_size_ordinal() + guides(colour = my_guide, shape = my_guide, size = my_guide) # You can merge an aesthetic that is already crossed with an aesthetic that # contributes to only one side of the cross. ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = paste(year, drv), shape = drv)) + guides( colour = guide_legend_cross(title = "My Title"), shape = guide_legend_cross(title = "My Title", key = "auto") )
# Standard use for single aesthetic. The default is to split labels to # disentangle aesthetics that are already crossed (by e.g. `paste()`) ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = paste(year, drv))) + guides(colour = "legend_cross") # If legends should be merged between identical aesthetics, both need the # same legend type. ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = paste(year, drv), shape = paste(year, drv))) + guides(colour = "legend_cross", shape = "legend_cross") # Crossing two aesthetics requires a shared title and `key = "auto"`. The # easy way to achieve this is to predefine a shared guide. my_guide <- guide_legend_cross(key = "auto", title = "My title") ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = drv, shape = factor(year))) + guides(colour = my_guide, shape = my_guide) # You can cross more than 2 aesthetics but not more than 2 unique aesthetics. ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = drv, shape = factor(year), size = factor(drv))) + scale_size_ordinal() + guides(colour = my_guide, shape = my_guide, size = my_guide) # You can merge an aesthetic that is already crossed with an aesthetic that # contributes to only one side of the cross. ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = paste(year, drv), shape = drv)) + guides( colour = guide_legend_cross(title = "My Title"), shape = guide_legend_cross(title = "My Title", key = "auto") )
This legend resembles ggplot2::guide_legend()
, but has the ability to
keep groups in blocks with their own titles.
guide_legend_group( key = "group_split", title = waiver(), override.aes = list(), nrow = NULL, ncol = NULL, theme = NULL, position = NULL, direction = NULL, order = 0 )
guide_legend_group( key = "group_split", title = waiver(), override.aes = list(), nrow = NULL, ncol = NULL, theme = NULL, position = NULL, direction = NULL, order = 0 )
key |
A group key specification. Defaults to
|
title |
A |
override.aes |
A named |
nrow , ncol
|
A positive |
theme |
A |
position |
A |
direction |
A |
order |
A positive |
A <GuideLegend>
object.
Other standalone guides:
guide_axis_base()
,
guide_axis_nested()
,
guide_colbar()
,
guide_colring()
,
guide_colsteps()
,
guide_legend_base()
,
guide_legend_cross()
Other legend guides:
guide_legend_base()
,
guide_legend_cross()
# Standard plot for selection of `msleep` df <- msleep[c(9, 28, 11, 5, 34, 54, 64, 24, 53), ] p <- ggplot(df) + aes(bodywt, awake, colour = paste(order, name)) + geom_point() # By default, groups are inferred from the name p + guides(colour = "legend_group") # You can also use a look-up table for groups # The lookup table can be more expansive than just the data: # We're using the full 'msleep' data here instead of the subset lut <- key_group_lut(msleep$name, msleep$order) p + aes(colour = name) + guides(colour = guide_legend_group(key = lut)) # `nrow` and `ncol` apply within groups p + guides(colour = guide_legend_group(nrow = 1)) # Groups are arranged according to `direction` p + guides(colour = guide_legend_group(ncol = 1, direction = "horizontal")) + theme(legend.title.position = "top") # Customising the group titles p + guides(colour = "legend_group") + theme( legendry.legend.subtitle.position = "left", legendry.legend.subtitle = element_text( hjust = 1, vjust = 1, size = rel(0.9), margin = margin(t = 5.5, r = 5.5) ) ) # Changing the spacing between groups p + guides(colour = "legend_group") + theme(legendry.group.spacing = unit(0, "cm"))
# Standard plot for selection of `msleep` df <- msleep[c(9, 28, 11, 5, 34, 54, 64, 24, 53), ] p <- ggplot(df) + aes(bodywt, awake, colour = paste(order, name)) + geom_point() # By default, groups are inferred from the name p + guides(colour = "legend_group") # You can also use a look-up table for groups # The lookup table can be more expansive than just the data: # We're using the full 'msleep' data here instead of the subset lut <- key_group_lut(msleep$name, msleep$order) p + aes(colour = name) + guides(colour = guide_legend_group(key = lut)) # `nrow` and `ncol` apply within groups p + guides(colour = guide_legend_group(nrow = 1)) # Groups are arranged according to `direction` p + guides(colour = guide_legend_group(ncol = 1, direction = "horizontal")) + theme(legend.title.position = "top") # Customising the group titles p + guides(colour = "legend_group") + theme( legendry.legend.subtitle.position = "left", legendry.legend.subtitle = element_text( hjust = 1, vjust = 1, size = rel(0.9), margin = margin(t = 5.5, r = 5.5) ) ) # Changing the spacing between groups p + guides(colour = "legend_group") + theme(legendry.group.spacing = unit(0, "cm"))
Guide composition is a meta-guide orchestrating an ensemble of other guides. On their own, a 'composing' guide is not very useful as a visual reflection of a scale.
new_compose( guides, args = list(), ..., available_aes = c("any", "x", "y", "r", "theta"), call = caller_env(), super = Compose )
new_compose( guides, args = list(), ..., available_aes = c("any", "x", "y", "r", "theta"), call = caller_env(), super = Compose )
guides |
A
|
args |
A |
... |
Additional parameters to pass on to
|
available_aes |
A |
call |
A call to display in messages. |
super |
A |
A <Compose>
(sub-)class guide that composes other guides.
Other composition:
compose_crux()
,
compose_ontop()
,
compose_sandwich()
,
compose_stack()
# The `new_compose()` function is not intended to be used directly my_composition <- new_compose(list("axis", "axis"), super = ComposeStack) # Is the same as my_composition <- compose_stack("axis", "axis")
# The `new_compose()` function is not intended to be used directly my_composition <- new_compose(list("axis", "axis"), super = ComposeStack) # Is the same as my_composition <- compose_stack("axis", "axis")
Guide gizmos are a speciality guide components that are very specific to one or a few aesthetics to display.
Typically they can be composed with other guides or guide primitives to form a complete guide.
Guide primitives are the building blocks of more complex guides. On their own, they are not very useful as a visual reflection of a scale.
Their purpose is to be combined with one another to form a more complex, complete guides that do reflect a scale in some way.
The guide primitives are simple, but flexible in that they are not tailored for one particular aesthetic. That way they can be reused and combined at will.
These functions are helper functions for working with grouped data as keys in guides. They all share the goal of creating a guide key, but have different methods.
key_group_split()
is a function factory whose functions make an attempt
to infer groups from the scale's labels.
key_group_lut()
is a function factory whose functions use a look up table
to sort out group membership.
key_group_split(sep = "[^[:alnum:]]+", reverse = FALSE) key_group_lut(members, group, ungrouped = "Other")
key_group_split(sep = "[^[:alnum:]]+", reverse = FALSE) key_group_lut(members, group, ungrouped = "Other")
sep |
A |
reverse |
A |
members |
A vector including the scale's |
group |
A vector parallel to |
ungrouped |
A |
A function to use as the key
argument in a guide.
Other keys:
key_range
,
key_specialty
,
key_standard
# Example scale values <- c("group A:value 1", "group A:value 2", "group B:value 1") template <- scale_colour_discrete(limits = values) # Treat the 'group X' part as groups key <- key_group_split(sep = ":") key(template) # Treat the 'value X' part as groups key <- key_group_split(sep = ":", reverse = TRUE) key(template) # Example scale template <- scale_colour_discrete(limits = msleep$name[c(1, 7, 9, 23, 24)]) # A lookup table can have more entries than needed key <- key_group_lut(msleep$name, msleep$order) key(template) # Or less entries than needed key <- key_group_lut( msleep$name[23:24], msleep$order[23:24], ungrouped = "Other animals" ) key(template)
# Example scale values <- c("group A:value 1", "group A:value 2", "group B:value 1") template <- scale_colour_discrete(limits = values) # Treat the 'group X' part as groups key <- key_group_split(sep = ":") key(template) # Treat the 'value X' part as groups key <- key_group_split(sep = ":", reverse = TRUE) key(template) # Example scale template <- scale_colour_discrete(limits = msleep$name[c(1, 7, 9, 23, 24)]) # A lookup table can have more entries than needed key <- key_group_lut(msleep$name, msleep$order) key(template) # Or less entries than needed key <- key_group_lut( msleep$name[23:24], msleep$order[23:24], ungrouped = "Other animals" ) key(template)
These functions are helper functions for working with ranged data as keys in guides. They all share the goal creating of a guide key, but have different methods:
key_range_auto()
is a function factory whose functions make an attempt
to infer ranges from the scale's labels.
key_range_manual()
uses user-provided vectors to set ranges.
key_range_map()
makes mappings from a <data.frame>
to set ranges.
key_range_auto(sep = "[^[:alnum:]]+", reverse = FALSE, ...) key_range_manual(start, end, name = NULL, level = NULL, ...) key_range_map(data, ..., .call = caller_env())
key_range_auto(sep = "[^[:alnum:]]+", reverse = FALSE, ...) key_range_manual(start, end, name = NULL, level = NULL, ...) key_range_map(data, ..., .call = caller_env())
sep |
A |
reverse |
A |
... |
|
start , end
|
A vector that can be interpreted by the scale, giving the start and end positions of each range respectively. |
name |
A |
level |
An |
data |
A |
.call |
A call to display in messages. |
The level
variable is optional and when missing, the guides use an
algorithm similar to IRanges::disjointBins()
to avoid overlaps.
The key_range_auto()
does not work with expression labels.
For key_range_auto()
a function. For key_range_manual()
and
key_range_map()
a <data.frame>
with the <key_range>
class.
Other keys:
key_group
,
key_specialty
,
key_standard
# Example scale template <- scale_x_discrete(limits = c("A 1", "B 1", "C&1", "D&2", "E&2")) # By default, splits on all non-alphanumeric characters auto <- key_range_auto() auto(template) # Only split on a specific character auto <- key_range_auto(sep = "&") auto(template) # Treating the letters as outer labels and numbers as inner labels auto <- key_range_auto(reverse = TRUE) auto(template) # Providing custom values key_range_manual( start = c(1, 5, 10), end = c(4, 15, 11), level = c(0, 2, 1), name = c("A", "B", "C") ) # Values from a <data.frame> key_range_map(presidential, start = start, end = end, name = name)
# Example scale template <- scale_x_discrete(limits = c("A 1", "B 1", "C&1", "D&2", "E&2")) # By default, splits on all non-alphanumeric characters auto <- key_range_auto() auto(template) # Only split on a specific character auto <- key_range_auto(sep = "&") auto(template) # Treating the letters as outer labels and numbers as inner labels auto <- key_range_auto(reverse = TRUE) auto(template) # Providing custom values key_range_manual( start = c(1, 5, 10), end = c(4, 15, 11), level = c(0, 2, 1), name = c("A", "B", "C") ) # Values from a <data.frame> key_range_map(presidential, start = start, end = end, name = name)
These functions are helper functions for working with keys in guides. The functions described here are not widely applicable and may only apply to a small subset of guides. As such, it is fine to adjust the arguments of a speciality key, but swapping types is ill-advised.
key_sequence()
is a function factory whose functions create a regularly
spaced sequence between the limits of a scale. It is used in colour bar
guides.
key_bins()
is a function factory whose function create a binned key
given the breaks in the scale. It is used in colour steps guides.
key_sequence(n = 15) key_bins(even.steps = FALSE, show.limits = NULL)
key_sequence(n = 15) key_bins(even.steps = FALSE, show.limits = NULL)
n |
A positive |
even.steps |
A |
show.limits |
A |
For key_sequence()
a function.
Other keys:
key_group
,
key_range
,
key_standard
# An example scale template <- scale_fill_viridis_c(limits = c(0, 10), breaks = c(2, 4, 6, 8)) # Retrieving colourbar and colourstep keys key_sequence()(template) key_bins()(template)
# An example scale template <- scale_fill_viridis_c(limits = c(0, 10), breaks = c(2, 4, 6, 8)) # Retrieving colourbar and colourstep keys key_sequence()(template) key_bins()(template)
These functions are helper functions for working with tick marks as keys in guides. They all share the goal of creating a guide key, but have different outcomes:
key_auto()
is a function factory whose functions extract a typical
key from major breaks in a scale.
key_manual()
uses user-provided vectors to make a key.
key_map()
makes mappings from a <data.frame>
to make a key.
key_minor()
is a function factory whose functions also extract minor
break positions for minor tick marks.
key_log()
is a function factory whose functions place ticks at intervals
in log10 space.
key_none()
makes an empty key with no entries.
key_auto(...) key_manual( aesthetic, value = aesthetic, label = as.character(value), type = NULL, ... ) key_map(data, ..., .call = caller_env()) key_minor(...) key_log( prescale_base = NULL, negative_small = 0.1, expanded = TRUE, labeller = NULL, ... ) key_none()
key_auto(...) key_manual( aesthetic, value = aesthetic, label = as.character(value), type = NULL, ... ) key_map(data, ..., .call = caller_env()) key_minor(...) key_log( prescale_base = NULL, negative_small = 0.1, expanded = TRUE, labeller = NULL, ... ) key_none()
... |
|
aesthetic , value
|
A vector of values for the guide to represent
equivalent to the |
label |
A |
type |
A |
data |
A |
.call |
A call to display in messages. |
prescale_base |
A |
negative_small |
A |
expanded |
A |
labeller |
A |
For key_auto()
, key_minor()
and key_log()
a function. For
key_manual()
and key_map()
a <data.frame>
with the <key_standard>
class.
Other keys:
key_group
,
key_range
,
key_specialty
# An example scale template <- scale_x_continuous(limits = c(0, 10)) # The auto, minor and log keys operate on scales key_auto()(template) key_minor()(template) # So does the log key template <- scale_x_continuous(transform = "log10", limits = c(0.1, 10)) key_log()(template) # Providing custom values key_manual( aesthetic = 1:5, label = c("one", "two", "three", "four", "five") ) # Values from a `<data.frame>` key_map(ToothGrowth, aesthetic = unique(supp)) # Empty key key_none()
# An example scale template <- scale_x_continuous(limits = c(0, 10)) # The auto, minor and log keys operate on scales key_auto()(template) key_minor()(template) # So does the log key template <- scale_x_continuous(transform = "log10", limits = c(0.1, 10)) key_log()(template) # Providing custom values key_manual( aesthetic = 1:5, label = c("one", "two", "three", "four", "five") ) # Values from a `<data.frame>` key_map(ToothGrowth, aesthetic = unique(supp)) # Empty key key_none()
This function constructs a boxes guide primitive.
primitive_box( key = "range_auto", angle = waiver(), oob = "squish", drop_zero = TRUE, pad_discrete = 0.4, min_size = NULL, levels_box = NULL, levels_text = NULL, theme = NULL, position = waiver() )
primitive_box( key = "range_auto", angle = waiver(), oob = "squish", drop_zero = TRUE, pad_discrete = 0.4, min_size = NULL, levels_box = NULL, levels_text = NULL, theme = NULL, position = waiver() )
key |
A range key specification. See more information in the linked topic. |
angle |
A specification for the text angle. Compared to setting the
|
oob |
A method for dealing with out-of-bounds (oob) ranges. Can be one
of |
drop_zero |
A |
pad_discrete |
A |
min_size |
A [ |
levels_box |
A list of |
levels_text |
A list of |
theme |
A |
position |
A |
A <PrimitiveBox>
primitive guide that can be used inside other
guides.
Below are the theme options that determine the styling of this guide, which may differ depending on whether the guide is used in an axis or in a legend context.
Common to both types is the following:
legendry.box
an <element_rect>
for the boxes
to draw.
axis.text.{x/y}.{position}
an <element_text>
for the text inside the boxes.
legend.text
an <element_text>
for the text
inside the boxes.
Other primitives:
primitive_bracket()
,
primitive_fence()
,
primitive_labels()
,
primitive_line()
,
primitive_spacer()
,
primitive_ticks()
,
primitive_title()
# A standard plot p <- ggplot(mpg, aes(interaction(drv, year), displ)) + geom_point() key <- key_range_manual(c(2, 4), c(5, 6), c("A", "B")) # Adding as secondary guides p + guides( x.sec = primitive_box(), y.sec = primitive_box(key = key) )
# A standard plot p <- ggplot(mpg, aes(interaction(drv, year), displ)) + geom_point() key <- key_range_manual(c(2, 4), c(5, 6), c("A", "B")) # Adding as secondary guides p + guides( x.sec = primitive_box(), y.sec = primitive_box(key = key) )
This function constructs a brackets guide primitive.
primitive_bracket( key = "range_auto", bracket = "line", angle = waiver(), oob = "squish", drop_zero = TRUE, pad_discrete = 0.4, levels_brackets = NULL, levels_text = NULL, theme = NULL, position = waiver() )
primitive_bracket( key = "range_auto", bracket = "line", angle = waiver(), oob = "squish", drop_zero = TRUE, pad_discrete = 0.4, levels_brackets = NULL, levels_text = NULL, theme = NULL, position = waiver() )
key |
A range key specification. See more information in the linked topic. |
bracket |
A bracket by providing one of the following:
|
angle |
A specification for the text angle. Compared to setting the
|
oob |
A method for dealing with out-of-bounds (oob) ranges. Can be one
of |
drop_zero |
A |
pad_discrete |
A |
levels_brackets |
A list of |
levels_text |
A list of |
theme |
A |
position |
A |
A <PrimitiveBracket>
primitive guide that can be used inside other
guides.
Below are the theme options that determine the styling of this guide, which may differ depending on whether the guide is used in an axis or a legend context.
Common to both types is the following:
legendry.bracket
an <element_line>
for the
line used to draw the brackets.
legendry.backet.size
a <unit>
setting the space afforded
to a bracket.
axis.text.{x/y}.{position}
an <element_text>
for the text displayed over the brackets.
legend.text
an <element_text>
for the text
displayed over the brackets.
Other primitives:
primitive_box()
,
primitive_fence()
,
primitive_labels()
,
primitive_line()
,
primitive_spacer()
,
primitive_ticks()
,
primitive_title()
# A standard plot p <- ggplot(mpg, aes(interaction(drv, year), displ)) + geom_point() key <- key_range_manual(c(2, 4), c(5, 6), c("A", "B")) # Adding as secondary guides p + guides( x.sec = primitive_bracket(), y.sec = primitive_bracket(key = key) )
# A standard plot p <- ggplot(mpg, aes(interaction(drv, year), displ)) + geom_point() key <- key_range_manual(c(2, 4), c(5, 6), c("A", "B")) # Adding as secondary guides p + guides( x.sec = primitive_bracket(), y.sec = primitive_bracket(key = key) )
This function constructs a fence guide primitive. The customisation options are easier to understand if we view fence 'post' as the vertical pieces of a real world fence, and the 'rail' as the horizontal pieces.
primitive_fence( key = "range_auto", rail = "none", angle = waiver(), oob = "squish", drop_zero = TRUE, pad_discrete = 0.5, levels_text = NULL, levels_post = NULL, levels_rail = NULL, theme = NULL, position = waiver() )
primitive_fence( key = "range_auto", rail = "none", angle = waiver(), oob = "squish", drop_zero = TRUE, pad_discrete = 0.5, levels_text = NULL, levels_post = NULL, levels_rail = NULL, theme = NULL, position = waiver() )
key |
A range key specification. See more information in the linked topic. |
rail |
A |
angle |
A specification for the text angle. Compared to setting the
|
oob |
A method for dealing with out-of-bounds (oob) ranges. Can be one
of |
drop_zero |
A |
pad_discrete |
A |
levels_text |
A list of |
levels_post , levels_rail
|
A list of |
theme |
A |
position |
A |
A <PrimitiveFence>
primitive guie that can be used inside other
guides.
Below are the theme options that determine the styling of this guide, which may differ depending on whether the guide is used in an axis or legend context.
Common to both types is the following:
legendry.fence.post
an <element_line>
for the
line used to draw the pieces orthogonal to the direction of the scale.
legendry.fence.rail
an <element_line>
for the
line used to draw the pieces parallel to the direction of the scale.
axis.text.{x/y}.{position}
an <element_text>
for the text displayed.
legend.text
an <element_text>
for the text
displayed.
Other primitives:
primitive_box()
,
primitive_bracket()
,
primitive_labels()
,
primitive_line()
,
primitive_spacer()
,
primitive_ticks()
,
primitive_title()
# A standard plot p <- ggplot(mpg, aes(interaction(drv, year), displ)) + geom_point() key <- key_range_manual(c(2, 4), c(5, 6), c("A", "B")) # Adding as secondary guides p + guides( x.sec = primitive_fence(rail = "inner"), y.sec = primitive_fence(key = key, rail = "outer") )
# A standard plot p <- ggplot(mpg, aes(interaction(drv, year), displ)) + geom_point() key <- key_range_manual(c(2, 4), c(5, 6), c("A", "B")) # Adding as secondary guides p + guides( x.sec = primitive_fence(rail = "inner"), y.sec = primitive_fence(key = key, rail = "outer") )
This function constructs a labels guide primitive.
primitive_labels( key = NULL, angle = waiver(), n.dodge = 1, check.overlap = FALSE, theme = NULL, position = waiver() )
primitive_labels( key = NULL, angle = waiver(), n.dodge = 1, check.overlap = FALSE, theme = NULL, position = waiver() )
key |
A standard key specification. See more information in the linked topic. |
angle |
A specification for the text angle. Compared to setting the
|
n.dodge |
An positive |
check.overlap |
A |
theme |
A |
position |
A |
A <PrimitiveLabels>
primitive guide that can be used inside other
guides.
Below are the theme options that determine the styling of this guide, which may differ depending on whether the guide is used in an axis or in a legend context.
axis.text.{x/y}.{position}
an <element_text>
for the display of the labels.
legend.text
an <element_text>
for the display
of the labels.
Other primitives:
primitive_box()
,
primitive_bracket()
,
primitive_fence()
,
primitive_line()
,
primitive_spacer()
,
primitive_ticks()
,
primitive_title()
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point() # Adding as secondary guides p + guides( x.sec = primitive_labels(), y.sec = primitive_labels(n.dodge = 2) )
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point() # Adding as secondary guides p + guides( x.sec = primitive_labels(), y.sec = primitive_labels(n.dodge = 2) )
This function constructs a line guide primitive.
primitive_line(key = NULL, cap = "none", theme = NULL, position = waiver())
primitive_line(key = NULL, cap = "none", theme = NULL, position = waiver())
key |
A standard key specification. See more information in the linked topic. |
cap |
A method to cap the axes. One of the following:
|
theme |
A |
position |
A |
A PrimitiveLine
primitive guide that can be used inside other
guides.
Below are the theme options that determine the styling of this guide, which may differ depending on whether the guide is used in an axis or in a legend context.
axis.line.{x/y}.{position}
an <element_line>
for the line style.
legend.axis.line
an <element_line>
for the
line style.
Other primitives:
primitive_box()
,
primitive_bracket()
,
primitive_fence()
,
primitive_labels()
,
primitive_spacer()
,
primitive_ticks()
,
primitive_title()
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point() + theme(axis.line = element_line()) # Adding as secondary guides p + guides( x.sec = primitive_line(), y.sec = primitive_line(cap = "both") )
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point() + theme(axis.line = element_line()) # Adding as secondary guides p + guides( x.sec = primitive_line(), y.sec = primitive_line(cap = "both") )
This function constructs a spacer guide primitive.
primitive_spacer( space = NULL, title = waiver(), theme = NULL, position = waiver() )
primitive_spacer( space = NULL, title = waiver(), theme = NULL, position = waiver() )
space |
A [ |
title |
A |
theme |
A |
position |
A |
A <PrimitiveSpacer>
primitive guide that can be used inside
other guides.
#' Below are the theme options that determine the styling of this guide. This guide does not have option dependent on its role as axis or legend.
legendry.guide.spacing
A <unit>
setting the amount of
spacing when the space
argument is NULL
.
Other primitives:
primitive_box()
,
primitive_bracket()
,
primitive_fence()
,
primitive_labels()
,
primitive_line()
,
primitive_ticks()
,
primitive_title()
ggplot(mpg, aes(displ, hwy)) + geom_point() + guides( x = guide_axis_stack("axis", primitive_spacer(unit(1, "cm")), "axis") )
ggplot(mpg, aes(displ, hwy)) + geom_point() + guides( x = guide_axis_stack("axis", primitive_spacer(unit(1, "cm")), "axis") )
This function constructs a ticks guide primitive.
primitive_ticks(key = NULL, bidi = FALSE, theme = NULL, position = waiver())
primitive_ticks(key = NULL, bidi = FALSE, theme = NULL, position = waiver())
key |
A standard key specification. See more information in the linked topic. |
bidi |
A |
theme |
A |
position |
A |
A PrimitiveTicks
primitive guide that can be used inside other
guides.
Below are the theme options that determine the styling of this guide, which may differ depending on whether the guide is used in an axis or in a legend context.
Common to both types is the following:
axis.ticks.{x/y}.{position}
an <element_line>
for major tick lines.
axis.minor.ticks.{x/y}.{position}
an
<element_line>
for minor tick lines.
legendry.axis.mini.ticks
an <element_line>
internally inheriting from the minor ticks for the smallest ticks in e.g.
log axes.
axis.ticks.length.{x/y}.{position}
a <unit>
for the major
ticks length.
axis.minor.ticks.length.{x/y}.{position}
a <unit>
for the
minor ticks length.
legendry.axis.mini.ticks.length
a <unit>
internally
inheriting from the minor tick length for the smallest ticks in e.g.
log axes.
legend.ticks
an <element_line>
for major tick
lines.
legendry.legend.minor.ticks
an <element_line>
for minor tick lines.
legendry.legend.mini.ticks
an <element_line>
for the smallest ticks in e.g. log axes.
legend.ticks.length
a <unit>
for the major ticks length.
legendry.legend.minor.ticks.length
a <unit>
for the
minor ticks length.
legendry.legend.mini.ticks.length
a <unit>
for the
smallest ticks in e.g. log axes.
Other primitives:
primitive_box()
,
primitive_bracket()
,
primitive_fence()
,
primitive_labels()
,
primitive_line()
,
primitive_spacer()
,
primitive_title()
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point() # Adding as secondary guides p + guides(x.sec = primitive_ticks(), y.sec = primitive_ticks())
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point() # Adding as secondary guides p + guides(x.sec = primitive_ticks(), y.sec = primitive_ticks())
This function constructs a title guide primitive.
primitive_title( title = waiver(), angle = waiver(), theme = NULL, position = waiver() )
primitive_title( title = waiver(), angle = waiver(), theme = NULL, position = waiver() )
title |
A |
angle |
A specification for the text angle. Compared to setting the
|
theme |
A |
position |
A |
A <PrimitiveTitle>
primitive guide that can be used inside other
guides.
Below are the theme options that determine the styling of this guide, which may differ depending on whether the guide is used in an axis or in a legend context.
axis.title.{x/y}.{position}
an <element_text>
for the title display.
legend.title
an <element_text>
for the title display.
Other primitives:
primitive_box()
,
primitive_bracket()
,
primitive_fence()
,
primitive_labels()
,
primitive_line()
,
primitive_spacer()
,
primitive_ticks()
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point() # Adding as secondary guides p + guides( x.sec = primitive_title("Horizontal Title"), y.sec = primitive_title(c("along vertical", "Multiple tiles")) )
# A standard plot p <- ggplot(mpg, aes(displ, hwy)) + geom_point() # Adding as secondary guides p + guides( x.sec = primitive_title("Horizontal Title"), y.sec = primitive_title(c("along vertical", "Multiple tiles")) )
This function has shorthand names for theme elements relating to guides. It
is intended to be used as the guide_*(theme)
argument. Because of this
intent, and due to legends and axes having mutually exclusive theme elements,
this function sets the elements for both simultaneously.
theme_guide( text = NULL, line = NULL, title = NULL, subtitle = NULL, text.position = NULL, title.position = NULL, subtitle.position = NULL, ticks = NULL, minor.ticks = NULL, mini.ticks = NULL, ticks.length = NULL, minor.ticks.length = NULL, mini.ticks.length = NULL, spacing = NULL, group.spacing = NULL, key = NULL, key.size = NULL, key.width = NULL, key.height = NULL, key.spacing = NULL, key.spacing.x = NULL, key.spacing.y = NULL, frame = NULL, byrow = NULL, background = NULL, margin = NULL, bracket = NULL, bracket.size = NULL, box = NULL, fence = NULL, fence.post = NULL, fence.rail = NULL )
theme_guide( text = NULL, line = NULL, title = NULL, subtitle = NULL, text.position = NULL, title.position = NULL, subtitle.position = NULL, ticks = NULL, minor.ticks = NULL, mini.ticks = NULL, ticks.length = NULL, minor.ticks.length = NULL, mini.ticks.length = NULL, spacing = NULL, group.spacing = NULL, key = NULL, key.size = NULL, key.width = NULL, key.height = NULL, key.spacing = NULL, key.spacing.x = NULL, key.spacing.y = NULL, frame = NULL, byrow = NULL, background = NULL, margin = NULL, bracket = NULL, bracket.size = NULL, box = NULL, fence = NULL, fence.post = NULL, fence.rail = NULL )
text |
An |
line |
An |
title |
An |
subtitle |
An |
text.position , title.position , subtitle.position
|
One of
|
ticks |
An |
minor.ticks |
An |
mini.ticks |
An |
ticks.length , minor.ticks.length , mini.ticks.length
|
A
[
|
spacing , group.spacing
|
A [ |
key |
An |
key.size , key.width , key.height
|
A |
key.spacing , key.spacing.x , key.spacing.y
|
A [ |
frame |
An |
byrow |
A |
background |
An |
margin |
A |
bracket |
An |
bracket.size |
A [ |
box |
An |
fence , fence.post , fence.rail
|
An
|
A <theme>
object that can be provided to a guide.
red_ticks <- theme_guide(ticks = element_line(colour = "red", linewidth = 0.5)) # Both axis and colourbar gain red ticks ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + guides( colour = guide_colourbar(theme = red_ticks), x = guide_axis(theme = red_ticks) )
red_ticks <- theme_guide(ticks = element_line(colour = "red", linewidth = 0.5)) # Both axis and colourbar gain red ticks ggplot(mpg, aes(displ, hwy, colour = cty)) + geom_point() + guides( colour = guide_colourbar(theme = red_ticks), x = guide_axis(theme = red_ticks) )