| Title: | Publication-Quality 'ggplot2' Annotation |
|---|---|
| Description: | Annotation helper functions for publication-quality 'ggplot2' visualisation. These functions make it easier to annotate plots in a way that stays consistent with the set theme. |
| Authors: | David Hodge [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-3868-7501>) |
| Maintainer: | David Hodge <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1.9000 |
| Built: | 2026-06-09 03:51:56 UTC |
| Source: | https://github.com/davidhodge931/ggscribe |
Modifies a mapped colour (or fill) aesthetic for contrast against the fill (or colour) aesthetic.
Function can be spliced into ggplot2::aes with rlang::!!!.
aes_contrast(..., dark = NULL, light = NULL, aesthetic = "colour")aes_contrast(..., dark = NULL, light = NULL, aesthetic = "colour")
... |
Require named arguments (and support trailing commas). |
dark |
A dark colour. If NULL, derived from theme text or panel background. |
light |
A light colour. If NULL, derived from theme text or panel background. |
aesthetic |
The aesthetic to be modified for contrast. Either "colour" (default) or "fill". |
A ggplot2 aesthetic in ggplot2::aes.
library(ggplot2) library(dplyr) library(stringr) set_theme( ggrefine::theme_light( panel_heights = rep(unit(50, "mm"), 100), panel_widths = rep(unit(75, "mm"), 100), ) ) ggwidth::set_equiwidth(equiwidth = 1.75) mtcars |> count(cyl, am) |> mutate( am = if_else(am == 0, "Automatic", "Manual"), cyl = as.factor(cyl) ) |> ggplot(aes(x = am, y = n, colour = cyl, fill = cyl, label = n)) + geom_col( position = position_dodge2(preserve = "single", padding = 0.05), width = ggwidth::get_width(n = 2, n_dodge = 3), ) + scale_fill_discrete(palette = jumble::jumble) + scale_colour_discrete(palette = blends::multiply(jumble::jumble)) + geom_text( mapping = ggscribe::aes_contrast(), # or aes(!!!ggscribe::aes_contrast()), position = position_dodge2( width = ggwidth::get_width(n = 2, n_dodge = 3), padding = 0.05, preserve = "single"), vjust = 1.33, show.legend = FALSE, ) + scale_y_continuous(expand = expansion(c(0, 0.05))) + ggrefine::modern(x_type = "discrete") mtcars |> count(cyl, am) |> mutate( am = if_else(am == 0, "automatic", "manual"), am = stringr::str_to_sentence(am), cyl = as.factor(cyl) ) |> ggplot(aes(y = am, x = n, colour = cyl, fill = cyl, label = n)) + geom_col( position = position_dodge2(preserve = "single", padding = 0.05), width = ggwidth::get_width(n = 2, n_dodge = 3, orientation = "y"), ) + scale_fill_discrete(palette = jumble::jumble) + scale_colour_discrete(palette = blends::multiply(jumble::jumble)) + geom_text( mapping = ggscribe::aes_contrast(), # or aes(!!!ggscribe::aes_contrast()), position = position_dodge2( width = ggwidth::get_width(n = 2, n_dodge = 3, orientation = "y"), preserve = "single", padding = 0.05, ), hjust = 1.25, show.legend = FALSE, ) + scale_x_continuous(expand = expansion(c(0, 0.05))) + ggrefine::modern(y_type = "discrete")library(ggplot2) library(dplyr) library(stringr) set_theme( ggrefine::theme_light( panel_heights = rep(unit(50, "mm"), 100), panel_widths = rep(unit(75, "mm"), 100), ) ) ggwidth::set_equiwidth(equiwidth = 1.75) mtcars |> count(cyl, am) |> mutate( am = if_else(am == 0, "Automatic", "Manual"), cyl = as.factor(cyl) ) |> ggplot(aes(x = am, y = n, colour = cyl, fill = cyl, label = n)) + geom_col( position = position_dodge2(preserve = "single", padding = 0.05), width = ggwidth::get_width(n = 2, n_dodge = 3), ) + scale_fill_discrete(palette = jumble::jumble) + scale_colour_discrete(palette = blends::multiply(jumble::jumble)) + geom_text( mapping = ggscribe::aes_contrast(), # or aes(!!!ggscribe::aes_contrast()), position = position_dodge2( width = ggwidth::get_width(n = 2, n_dodge = 3), padding = 0.05, preserve = "single"), vjust = 1.33, show.legend = FALSE, ) + scale_y_continuous(expand = expansion(c(0, 0.05))) + ggrefine::modern(x_type = "discrete") mtcars |> count(cyl, am) |> mutate( am = if_else(am == 0, "automatic", "manual"), am = stringr::str_to_sentence(am), cyl = as.factor(cyl) ) |> ggplot(aes(y = am, x = n, colour = cyl, fill = cyl, label = n)) + geom_col( position = position_dodge2(preserve = "single", padding = 0.05), width = ggwidth::get_width(n = 2, n_dodge = 3, orientation = "y"), ) + scale_fill_discrete(palette = jumble::jumble) + scale_colour_discrete(palette = blends::multiply(jumble::jumble)) + geom_text( mapping = ggscribe::aes_contrast(), # or aes(!!!ggscribe::aes_contrast()), position = position_dodge2( width = ggwidth::get_width(n = 2, n_dodge = 3, orientation = "y"), preserve = "single", padding = 0.05, ), hjust = 1.25, show.legend = FALSE, ) + scale_x_continuous(expand = expansion(c(0, 0.05))) + ggrefine::modern(y_type = "discrete")
Draws one or more brackets along a floating axis line. Each bracket spans
min(breaks) to max(breaks) with caps at every break value.
Requires coord_cartesian(clip = "off").
axis_bracket( xintercept = NULL, yintercept = NULL, breaks, length = ggplot2::rel(1), colour = NULL, linewidth = NULL, linetype = NULL, layout = NULL )axis_bracket( xintercept = NULL, yintercept = NULL, breaks, length = ggplot2::rel(1), colour = NULL, linewidth = NULL, linetype = NULL, layout = NULL )
xintercept |
One or more x positions for vertical axis lines, in data
coordinates or wrapped in |
yintercept |
One or more y positions for horizontal axis lines, in data
coordinates or wrapped in |
breaks |
A numeric vector of length >= 2 in data coordinates, or
wrapped in |
length |
Length of the bracket caps. Supports |
colour |
Inherits from |
linewidth |
Inherits from |
linetype |
Inherits from |
layout |
Controls which panels the annotation appears in. |
Caps always point in the positive direction by default (right for
xintercept, up for yintercept). Use a negative length to flip them
(e.g. length = -rel(1)).
A list of ggplot2 annotation layers.
axis_line(), axis_ticks(),
axis_text(), reference_line(), panel_shade()
Draws a full line at one or more floating positions, with style defaults
taken from the axis.line element of the set theme. Requires
coord_cartesian(clip = "off").
axis_line( xintercept = NULL, yintercept = NULL, colour = NULL, linewidth = NULL, linetype = NULL, arrow = NULL, layout = NULL )axis_line( xintercept = NULL, yintercept = NULL, colour = NULL, linewidth = NULL, linetype = NULL, arrow = NULL, layout = NULL )
xintercept |
One or more x positions for vertical axis lines, in data
coordinates or wrapped in |
yintercept |
One or more y positions for horizontal axis lines, in data
coordinates or wrapped in |
colour |
Inherits from |
linewidth |
Inherits from |
linetype |
Inherits from |
arrow |
A |
layout |
Controls which panels the annotation appears in. |
The arrow (if any) points in the positive direction by default — rightward
for xintercept lines, upward for yintercept lines.
A list of ggplot2 annotation layers.
axis_ticks(), axis_text(),
axis_bracket(), reference_line(), panel_shade()
Draws text labels at specified break positions along a floating axis line,
with style defaults taken from the axis.text element of the set theme.
Requires coord_cartesian(clip = "off").
axis_text( xintercept = NULL, yintercept = NULL, breaks, labels = NULL, length = ggplot2::rel(1), angle = 0, hjust = NULL, vjust = NULL, colour = NULL, size = NULL, family = NULL, layout = NULL )axis_text( xintercept = NULL, yintercept = NULL, breaks, labels = NULL, length = ggplot2::rel(1), angle = 0, hjust = NULL, vjust = NULL, colour = NULL, size = NULL, family = NULL, layout = NULL )
xintercept |
One or more x positions for vertical axis lines, in data
coordinates or wrapped in |
yintercept |
One or more y positions for horizontal axis lines, in data
coordinates or wrapped in |
breaks |
A numeric vector of break positions in data coordinates, or
wrapped in |
labels |
One of:
|
length |
Offset from the axis line including tick length and margin.
Supports |
angle |
Text rotation angle. Defaults to |
hjust, vjust
|
Justification. Auto-calculated from axis direction and
|
colour |
Inherits from |
size |
Inherits from |
family |
Inherits from |
layout |
Controls which panels the annotation appears in. |
Text always sits on the positive side of the axis by default (right of
xintercept lines, above yintercept lines). Use a negative length
to place text on the opposite side (e.g. length = -rel(1)).
A list of ggplot2 annotation layers.
axis_line(), axis_ticks(),
axis_bracket(), reference_line(), panel_shade()
Draws axis ticks at specified break positions along a floating axis line.
Requires coord_cartesian(clip = "off").
axis_ticks( xintercept = NULL, yintercept = NULL, breaks, length = ggplot2::rel(1), colour = NULL, linewidth = NULL, linetype = NULL, arrow = NULL, layout = NULL )axis_ticks( xintercept = NULL, yintercept = NULL, breaks, length = ggplot2::rel(1), colour = NULL, linewidth = NULL, linetype = NULL, arrow = NULL, layout = NULL )
xintercept |
One or more x positions for vertical axis lines, in data
coordinates or wrapped in |
yintercept |
One or more y positions for horizontal axis lines, in data
coordinates or wrapped in |
breaks |
A numeric vector of break positions in data coordinates, or
wrapped in |
length |
Total tick length. Supports |
colour |
Inherits from |
linewidth |
Inherits from |
linetype |
Inherits from |
arrow |
A |
layout |
Controls which panels the annotation appears in. |
Ticks always point in the positive direction by default (right for
xintercept, up for yintercept). Use a negative length to flip them
(e.g. length = -rel(1)).
A list of ggplot2 annotation layers.
axis_line(), axis_text(),
axis_bracket(), reference_line(), panel_shade()
A wrapper around ggplot2::guide_axis() that defaults to making transparent
ticks and lines and making the text the same colour as the plot background fill
from the set theme.
guide_sec_axis_spacer(...)guide_sec_axis_spacer(...)
... |
Additional arguments passed to |
A guide object to be used in a scale's guide argument or within
sec_axis_text().
sec_axis_spacer(), axis_text(), axis_ticks(), axis_bracket()
A wrapper around ggplot2::guide_axis() that defaults to making transparent
ticks and lines while preserving text, making it ideal for annotation labels.
guide_sec_axis_text(..., theme = NULL)guide_sec_axis_text(..., theme = NULL)
... |
Additional arguments passed to |
theme |
A theme object to style the secondary axis. |
A guide object to be used in a scale's guide argument or within
sec_axis_text().
sec_axis_text(), axis_text(), axis_ticks(), axis_bracket()
Draws filled rectangles over the panel. Defaults to the panel.background
fill from the set theme at full opacity, making it useful for layering a
solid background over existing content. Should be placed before geom layers.
panel_background( xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill = NULL, alpha = 1, colour = NULL, linewidth = NULL, linetype = 0, layout = NULL )panel_background( xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill = NULL, alpha = 1, colour = NULL, linewidth = NULL, linetype = 0, layout = NULL )
xmin, xmax
|
Left and right edges of the rectangle in data coordinates.
Defaults to |
ymin, ymax
|
Bottom and top edges of the rectangle in data coordinates.
Defaults to |
fill |
Fill colour. Defaults to the |
alpha |
Opacity. Defaults to |
colour |
Border colour. Defaults to the resolved |
linewidth |
Inherits from |
linetype |
Border linetype. Defaults to |
layout |
Controls which panels the annotation appears in. |
A list containing annotation layers.
panel_shade(), axis_line(), reference_line()
Draws gridlines at specified positions, with style defaults taken from the
panel.grid.major element of the set theme. Crop bounds (xmin, xmax,
ymin, ymax) both filter which lines are drawn and control how far they
run across the panel.
panel_grid( xintercept = NULL, yintercept = NULL, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, colour = NULL, linewidth = NULL, linetype = NULL, layout = NULL )panel_grid( xintercept = NULL, yintercept = NULL, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, colour = NULL, linewidth = NULL, linetype = NULL, layout = NULL )
xintercept |
One or more x positions for vertical gridlines, in data
coordinates or wrapped in |
yintercept |
One or more y positions for horizontal gridlines, in data
coordinates or wrapped in |
xmin, xmax
|
Left and right crop bounds. Vertical gridlines outside
|
ymin, ymax
|
Bottom and top crop bounds. Horizontal gridlines outside
|
colour |
Inherits from |
linewidth |
Inherits from |
linetype |
Inherits from |
layout |
Controls which panels the annotation appears in. |
A list of ggplot2 annotation layers.
axis_line(), reference_line(), panel_shade()
A convenience wrapper around panel_background() with defaults suited to
subtle overlays: a neutral grey fill at 25% opacity with no border. Should
be placed before geom layers.
panel_shade( xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "#878580", alpha = 0.25, colour = "transparent", linewidth = NULL, linetype = 1, layout = NULL )panel_shade( xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "#878580", alpha = 0.25, colour = "transparent", linewidth = NULL, linetype = 1, layout = NULL )
xmin, xmax
|
Left and right edges of the rectangle in data coordinates.
Defaults to |
ymin, ymax
|
Bottom and top edges of the rectangle in data coordinates.
Defaults to |
fill |
Fill colour. Defaults to a neutral grey. May be a vector the same length as the bounds. |
alpha |
Opacity. Defaults to |
colour |
Border colour. Defaults to |
linewidth |
Inherits from |
linetype |
Border linetype. Defaults to |
layout |
Controls which panels the annotation appears in. |
A list containing annotation layers.
panel_background(), axis_line(), reference_line()
Draws one or more reference lines within the panel, with style defaults
taken from the axis.line element of the set theme.
reference_line( xintercept = NULL, yintercept = NULL, colour = NULL, linewidth = NULL, linetype = "dashed", arrow = NULL, layout = NULL )reference_line( xintercept = NULL, yintercept = NULL, colour = NULL, linewidth = NULL, linetype = "dashed", arrow = NULL, layout = NULL )
xintercept |
One or more x positions for vertical reference lines, in
data coordinates or wrapped in |
yintercept |
One or more y positions for horizontal reference lines, in
data coordinates or wrapped in |
colour |
Inherits from |
linewidth |
Inherits from |
linetype |
Defaults to |
arrow |
A |
layout |
Controls which panels the annotation appears in. |
The arrow (if any) points in the positive direction by default — rightward
for xintercept lines, upward for yintercept lines.
A list of ggplot2 annotation layers.
axis_line(), axis_ticks(),
axis_text(), axis_bracket(), panel_shade()
A convenience wrapper around sec_axis_text() that reserves vertical (or
horizontal) space above (or beside) an axis without drawing visible text.
Useful for pushing axis titles away from the panel to make room for
annotations added with axis_text(), axis_ticks(), or axis_bracket().
sec_axis_spacer( breaks = function(x) mean(x), labels = "", name = NULL, guide = guide_sec_axis_spacer(), ... )sec_axis_spacer( breaks = function(x) mean(x), labels = "", name = NULL, guide = guide_sec_axis_spacer(), ... )
breaks |
A function or numeric vector giving the break position(s) used
to anchor the text. Defaults to |
labels |
A character string used as the spacer. Defaults to |
name |
The name of the secondary axis. Use |
guide |
A guide object used to render the axis. Defaults to
|
... |
Additional arguments passed to |
A ggplot2::sec_axis() object.
guide_sec_axis_spacer(), axis_text(), axis_ticks(), axis_bracket()
Secondary axis for text annotations
sec_axis_text( breaks = function(x) mean(x), labels = ggplot2::waiver(), name = NULL, guide = guide_sec_axis_text(), ... )sec_axis_text( breaks = function(x) mean(x), labels = ggplot2::waiver(), name = NULL, guide = guide_sec_axis_text(), ... )
breaks |
A function or numeric vector giving the break position(s) used
to anchor the text. Defaults to |
labels |
One of:
|
name |
The name of the secondary axis. Use |
guide |
A guide object used to render the axis. Defaults to
|
... |
Additional arguments passed to |
A AxisSecondary object for use in the sec.axis argument of
scale_x_continuous() or scale_y_continuous().
guide_sec_axis_text(), axis_text(), axis_ticks(), axis_bracket()