| Title: | Universal Design-Oriented Enhancements for 'ggplot2' |
|---|---|
| Description: | A collection of enhancements to 'ggplot2', with a focus on creating Universally Designed, accessible graphs easily and quickly. |
| Authors: | Alex Bajcz [aut, cre] (ORCID: <https://orcid.org/0000-0002-5909-4676>) |
| Maintainer: | Alex Bajcz <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.5.6 |
| Built: | 2026-06-18 17:03:08 UTC |
| Source: | https://github.com/maisrc/ggplotplus |
Registers a custom point shape for use with geom_point_plus().
Custom shapes are defined by a data frame of connected polygon coordinates
and, once validated, are stored in a session-level shape registry.
add_shape_plus(name = NULL, shape = NULL, overwrite = FALSE, ...)add_shape_plus(name = NULL, shape = NULL, overwrite = FALSE, ...)
name |
A single character string giving the name of the new shape. |
shape |
A data frame with columns |
overwrite |
Logical. If |
... |
Additional arguments. Partial argument matching is supported for friendly UX. |
A shape must be supplied as a data frame with columns x, y,
and piece. The x and y columns define the vertices of
the shape, centered around 0, 0. Coordinates should generally be
scaled to about +/-0.4 to match the scaling of built-in point shapes.
The piece column identifies separate polygon pieces within the same
shape to generate "holes," as appropriate.
For inspiration and to model inputs, see ggplotplus_shapes_list for the
structure of the built-in point shapes.
Invisibly returns the registered shape name.
test_star = data.frame( x = c(0.000, 0.118, 0.380, 0.190, 0.235, 0.000, -0.235, -0.190, -0.380, -0.118), y = c(0.400, 0.124, 0.124, -0.047, -0.324, -0.153, -0.324, -0.047, 0.124, 0.124), piece = 1 ) add_shape_plus("test_star", test_star)test_star = data.frame( x = c(0.000, 0.118, 0.380, 0.190, 0.235, 0.000, -0.235, -0.190, -0.380, -0.118), y = c(0.400, 0.124, 0.124, -0.047, -0.324, -0.153, -0.324, -0.047, 0.124, 0.124), piece = 1 ) add_shape_plus("test_star", test_star)
direct_labels_plus() adds procedurally placed text labels to grouped
point or line plots as an alternative to using a legend, which might be
space-inefficient, off to one side away from where readers will see it,
or force readers to jump their focus long distances to align groups with
labels. The function is intended as a friendlier alternative to
manually placing group labels via ggplot2::annotate(). Labels are drawn
using ggrepel::geom_label_repel(), so they'll repel one another as well
as the plotted data they're labeling (within reason).
direct_labels_plus( data, x, y, group, placement = "top", geometry = "point", adj_fact = 0, key_labels = NULL, facet_vars = NULL, ... )direct_labels_plus( data, x, y, group, placement = "top", geometry = "point", adj_fact = 0, key_labels = NULL, facet_vars = NULL, ... )
data |
A data frame containing the variables to be both plotted and
labelled. Most often, this will be the same data frame as supplied to
|
x, y
|
Unquoted column names giving the x- and y-coordinates of the data to be plotted and thus also labelled. |
group |
Unquoted column name giving the grouping variable with which to label the underlying data. Must be a single unquoted column name and not an expression. |
placement |
Where labels should be placed relative to each group. One of "top", "right", "bottom", or "left". For geometry = "point", this controls the target location used to choose a representative point from each group. For geometry = "line", this chooses the endpoint or extreme point to label. Experimenting with different placements to find the one that works best for a particular graph is advised! |
geometry |
The kind of geometry being labelled. Currently supports "point" and "line". |
adj_fact |
A single numeric value controlling how far label targets are adjusted toward or away from the selected edge of each group. Values are interpreted as a proportion of the group-specific x- or y-range. Positive values move targets outward (towards the plot edge); negative values move them inward. For geometry = "point", this changes the target spot used to choose a particular point from each group to label, but does not move the final label anchor away from the selected point. In practice, this is likely not particularly useful for point geometries most of the time as a result. |
key_labels |
Optional replacement contents for the labels. May be one of:
|
facet_vars |
Optional character vector of max length 2 (or else NULL) giving one or two faceting variables you're using to facet your plot. When supplied, label locations are calculated separately within each group-by-facet combination. This is useful when adding direct labels to faceted plots. |
... |
Additional arguments passed along to
|
This function is experimental. It currently works best for ordinary scatterplots and grouped line/path plots where each group has a visually meaningful position in two-dimensional (x/y) space. It will not work for every ggplot2 geometry, statistic, coordinate system, or faceting arrangement.
For point geometries, direct_labels_plus() calculates one label location per group by finding the observed point closest to a group-specific target positioned towards one of the "edges" of a group's cluster of points. For "top" and "bottom" placement, the target is horizontally centered on the group's median x-value and vertically placed near the group's maximum or minimum y-value. For "left" and "right" placement, the same logic is applied with x and y reversed.
For line geometries, labels are placed at the group-specific endpoint or extreme value implied by placement: the largest x-value for "right", the smallest x-value for "left", the largest y-value for "top", and the smallest y-value for "bottom". The final label anchor may then be adjusted by adj_fact.
To help labels repel from plotted points or lines, the function silently adds empty-label rows at the original data coordinates. ggrepel does not draw these empty labels, but still uses their positions when placing visible labels. This helps to ensure, in general, that labels neither cover each other nor the underlying data they're labelling.
Currently, the function sets defaults for the following parameters of
ggrepel::geom_label_repel(): size (5), box.padding (0.5), max.overlaps
(Inf), segment.size (1), and min.segment.length (0). However, these are
overridable, if the user provides named arguments that at least partially
match.
Known limitations:
Label locations are calculated in the raw data space supplied to the function. Transformed scales, reversed axes, and non-Cartesian coordinate systems may give unexpected results. Pre-transform data rather than entering raw data and transforming later.
coord_flip(), coord_polar(), map projections, and other coordinate transformations are not currently supported.
The function places labels according to the data values supplied in
data, not those subsequently generated by ggplot2 stat functions. For
example, to label fitted smooth lines like those from
ggplot2::geom_smooth(), feed this function fitted values outputted from
such a function instead of the raw data.
Very dense plots or plots with many groups will likely still have overlapping or poorly placed labels. ggrepel helps, but it cannot make a crowded plot uncrowded. Alas.
Polygon, ribbon, area, segment, curve, and spatial geometries are not currently supported but might be in the future.
A ggplot2 layer produced by ggrepel::geom_label_repel().
ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Sepal.Width, color = Species)) + ggplot2::geom_point() + direct_labels_plus( data = iris, x = Sepal.Length, y = Sepal.Width, group = Species, placement = "right", geometry = "point" ) + ggplot2::guides(color = "none") line_data = ChickWeight |> dplyr::group_by(Diet, Time) |> dplyr::summarize(weight = mean(weight), .groups = "drop") ggplot2::ggplot(line_data, ggplot2::aes(Time, weight, color = Diet)) + ggplot2::geom_line(ggplot2::aes(group = Diet)) + direct_labels_plus( data = line_data, x = Time, y = weight, group = Diet, placement = "right", geometry = "line", adj_fact = 0.05 ) + ggplot2::guides(color = "none")ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Sepal.Width, color = Species)) + ggplot2::geom_point() + direct_labels_plus( data = iris, x = Sepal.Length, y = Sepal.Width, group = Species, placement = "right", geometry = "point" ) + ggplot2::guides(color = "none") line_data = ChickWeight |> dplyr::group_by(Diet, Time) |> dplyr::summarize(weight = mean(weight), .groups = "drop") ggplot2::ggplot(line_data, ggplot2::aes(Time, weight, color = Diet)) + ggplot2::geom_line(ggplot2::aes(group = Diet)) + direct_labels_plus( data = line_data, x = Time, y = weight, group = Diet, placement = "right", geometry = "line", adj_fact = 0.05 ) + ggplot2::guides(color = "none")
geom_jitter_plus() is a convenience wrapper around
geom_point_plus() that applies jittering to reduce overplotting.
It supports the same custom shape palette and fillable point rendering
as geom_point_plus() while exposing the familiar width,
height, and seed arguments used by
ggplot2::position_jitter().
geom_jitter_plus( mapping = NULL, data = NULL, stat = "identity", position = "jitter", ..., width = NULL, height = NULL, seed = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )geom_jitter_plus( mapping = NULL, data = NULL, stat = "identity", position = "jitter", ..., width = NULL, height = NULL, seed = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
... |
Additional arguments passed to |
width, height
|
Amount of horizontal and vertical jitter. Passed to
|
seed |
Random seed used by |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A ggplot2 layer.
ggplot2::ggplot(iris, ggplot2::aes(Species, Sepal.Length)) + geom_jitter_plus( ggplot2::aes(shape = Species, fill = Petal.Length), width = 0.15, seed = 1, colour = "black" ) ggplot2::ggplot(iris, ggplot2::aes(Species, Sepal.Length)) + geom_jitter_plus( ggplot2::aes(shape = Species), chosen_shapes = c("plus", "flower", "lotus"), seed = 123 )ggplot2::ggplot(iris, ggplot2::aes(Species, Sepal.Length)) + geom_jitter_plus( ggplot2::aes(shape = Species, fill = Petal.Length), width = 0.15, seed = 1, colour = "black" ) ggplot2::ggplot(iris, ggplot2::aes(Species, Sepal.Length)) + geom_jitter_plus( ggplot2::aes(shape = Species), chosen_shapes = c("plus", "flower", "lotus"), seed = 123 )
ggplot2 graph with new, distinctive shapes.This function behaves similarly to ggplot2::geom_point() except that it takes several new inputs: shapes, n_shapes, shape_values, legend_title, key_size, and show_shape_scale. These are explained below.
geom_point_plus( mapping = NULL, data = NULL, stat = "identity", position = "identity", avail_shapes = NULL, n_shapes = length(avail_shapes), chosen_shapes = NULL, legend_title = NULL, legend_labels = NULL, include_shape_legend = TRUE, ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, show_shape_scale = TRUE )geom_point_plus( mapping = NULL, data = NULL, stat = "identity", position = "identity", avail_shapes = NULL, n_shapes = length(avail_shapes), chosen_shapes = NULL, legend_title = NULL, legend_labels = NULL, include_shape_legend = TRUE, ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, show_shape_scale = TRUE )
mapping |
Set of aesthetic mappings created by aes(), as in |
data |
The data to be displayed in this layer, as in |
stat |
The statistical transformation to use on the data for this layer, as in |
position |
A position adjustment to use on the data for this layer, as in |
avail_shapes |
A named list of custom shapes to be drawn in place of |
n_shapes |
A length-1 integer corresponding to the number of distinct shapes the function is allowed to pull from the shapes palette specified to |
chosen_shapes |
A character string referring by name to elements in the current shapes registry that the function should use to allocate shapes to values, e.g. |
legend_title |
A length-1 character string corresponding to the name to be used for the shape legend title (if any). This is passed internally to |
legend_labels |
A character vector corresponding to the names to be used for the shape legend labels (if any). This is passed internally to |
include_shape_legend |
Logical indicating whether a shape legend will be shown (one is always shown unless this is set to FALSE, even when shape is being mapped to a constant and thus a legend may not be appropriate). |
... |
Other arguments passed on to this layer()'s params argument, as in |
na.rm |
Logical value controlling whether missing values should be removed from the data with a warning or silently, as in |
show.legend |
Logical value controlling whether this layer should be included in the legend(s), as in |
inherit.aes |
Logical controlling whether global aesthetics specified in |
show_shape_scale |
Logical controlling whether a call to |
Collectively, these inputs allow geom_point_plus() to access and draw several new and distinctive shapes that are designed to be more readily distinguishable from one another when shape communicates difference.
To see the special shapes available via this function run geom_point_plus_shapes().
Note: As of Version 0.5.2, shapes 21-25 in R's default shapes palette are now also available via geom_point_plus_shapes(); these are called "circle", "square", "diamond", "triangle_up", and "triangle_down", respectively, though they can also be referred to by number.
A ggplot2 layer object.
ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg, fill = drat)) + geom_point_plus(ggplot2::aes(shape = factor(gear)), size = 5) ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg, fill = factor(cyl))) + geom_point_plus(ggplot2::aes(shape = factor(carb)), shape_values = c("squircle", "lotus", "sunburst", "octagon", "cross", "oval"), size = 5, stroke = 0.4) ggplot2::ggplot(iris, ggplot2::aes(Petal.Width, Petal.Length, fill = Species)) + geom_point_plus(ggplot2::aes(shape = Species), size = 5, alpha = 0.7)ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg, fill = drat)) + geom_point_plus(ggplot2::aes(shape = factor(gear)), size = 5) ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg, fill = factor(cyl))) + geom_point_plus(ggplot2::aes(shape = factor(carb)), shape_values = c("squircle", "lotus", "sunburst", "octagon", "cross", "oval"), size = 5, stroke = 0.4) ggplot2::ggplot(iris, ggplot2::aes(Petal.Width, Petal.Length, fill = Species)) + geom_point_plus(ggplot2::aes(shape = Species), size = 5, alpha = 0.7)
A prebuilt ggplot object displaying the nine custom point shapes designed specifically for use in geom_point_plus(). As of Version 0.5.2, other shapes are also available, but those are not shown via this function. See the documentation for geom_point_plus() for details.
geom_point_plus_shapes()geom_point_plus_shapes()
A ggplot object.
Returns a ggplot2 graph showing the ggplotplus shapes palette.
geom_point_plus()
A named list of custom point shapes used by geom_point_plus(). Each element
contains x and y coordinates plus a piece identifier used when drawing
filled shapes and holes.
ggplotplus_shapes_listggplotplus_shapes_list
A named list of data frames. Each data frame has columns:
X coordinates for the shape outline.
Y coordinates for the shape outline.
Integer identifier for separate polygon pieces or holes.
names(ggplotplus_shapes_list)names(ggplotplus_shapes_list)
Convenience helper for converting ggplotplus plot objects into grobs using
ggplot2::ggplotGrob().
ggplotplus_to_cowplot(plot)ggplotplus_to_cowplot(plot)
plot |
A ggplot or ggplotplus plot object. |
This is primarily intended for compatibility with packages such as cowplot that may not yet fully recognize custom S7 plot subclasses during internal dispatch.
A grob object suitable for use with grid-based plotting utilities
such as cowplot::plot_grid().
p = ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Petal.Length, colour = Species)) + geom_point_plus() if(requireNamespace("cowplot", quietly = TRUE)) { cowplot::plot_grid( ggplotplus_to_cowplot(p), ggplotplus_to_cowplot(p) ) }p = ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Petal.Length, colour = Species)) + geom_point_plus() if(requireNamespace("cowplot", quietly = TRUE)) { cowplot::plot_grid( ggplotplus_to_cowplot(p), ggplotplus_to_cowplot(p) ) }
Convenience helper for converting ggplotplus plot objects into patchwork-compatible wrapped elements.
ggplotplus_to_patchwork(plot)ggplotplus_to_patchwork(plot)
plot |
A ggplot or ggplotplus plot object. |
This helper is primarily intended for compatibility with patchwork, whose plot-composition operators may not yet fully recognize custom S7 plot subclasses under ggplot2 4.x.
Internally, the plot is first converted into a grob using
ggplotplus_to_cowplot(), then wrapped using
patchwork::wrap_elements().
This helper requires the patchwork package, but ggplotplus does not import patchwork. Users only need patchwork installed if they want to compose ggplotplus plots with patchwork.
A patchwork-compatible wrapped plot element.
if(requireNamespace("patchwork", quietly = TRUE)) { p1 = ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Petal.Length, colour = Species)) + geom_point_plus() p2 = ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg, shape = factor(cyl))) + geom_point_plus() (ggplotplus_to_patchwork(p1) | ggplotplus_to_patchwork(p2)) + patchwork::plot_annotation( title = "ggplotplus + patchwork" ) }if(requireNamespace("patchwork", quietly = TRUE)) { p1 = ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Petal.Length, colour = Species)) + geom_point_plus() p2 = ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg, shape = factor(cyl))) + geom_point_plus() (ggplotplus_to_patchwork(p1) | ggplotplus_to_patchwork(p2)) + patchwork::plot_annotation( title = "ggplotplus + patchwork" ) }
Adds light and easily ignored major gridlines along only axes mapped to continuous variables. Minor gridlines are blanked. This enables the benefits of gridlines (in instances where there are some) but minimizes visual clutter and cognitive load.
gridlines_plus( color = "gray90", linewidth = 1.2, linetype = "solid", notx = FALSE, noty = FALSE, override_legend_alphasize = TRUE, enable_coaching = TRUE )gridlines_plus( color = "gray90", linewidth = 1.2, linetype = "solid", notx = FALSE, noty = FALSE, override_legend_alphasize = TRUE, enable_coaching = TRUE )
color |
Gridline color. Single character string. Default: |
linewidth |
Gridline width (theme line units). Single numeric. Default: |
linetype |
Gridline type. Single string (e.g., |
notx, noty
|
Logicals indicating whether gridlines should not be drawn in a specific direction (i.e., the user wants those to be blank even when the axis is continuous). Default to FALSE (gridlines are added). |
override_legend_alphasize |
Logical indicating whether, for any fill, color, and/or shape legends, to override the size and/or alpha values to the ggplotplus default values (1 and 5, respectively). Defaults to TRUE. |
enable_coaching |
Logical indicating whether ggplotplus should perform certain automated checks for possible non-Universal graph design and provide coaching messages when these are detected. Defaults to TRUE. |
gridlines_plus() ignores any theme instructions to blank major panel scales in either the x or y direction via, e.g., theme_plus(panel.grid.major.y = ggplot2::element_blank()). Instead, users can set, e.g., noty == TRUE to prevent gridlines from being drawn in a specific direction, even if that scale is continuous. Similarly, gridline color, linewidth, and linetypes should be set directly in gridlines_plus() instead of via theme(). Trying to do the latter will fail without error or warning!
Under the hood, gridlines_plus() checks layer and/or global mappings to
see if x and/or y are continuous. It does this by inspecting the trained panel scales.
It then turns on major gridlines for continuous directions and explicitly
blanks gridlines for other axes (as well as all minor gridlines).
One interaction exists between gridlines_plus() and theme_plus(): If using
them in tandem, the linewidth provided to gridlines_plus() will be a
function of the scaling factor generated according to the export_width and
export_height inputs provided to theme_plus(). That is to say that the
final gridline widths may be bigger or small than those specified in
gridlines_plus() as a function of your intended output size.
An ggplot class object for adding to a plot with +.
library(ggplot2) ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Petal.Length)) + ggplot2::geom_point() + theme_plus() + #WE DON'T RECOMMEND USING gridlines_plus() WITHOUT ALSO USING theme_plus() gridlines_plus() # Only y is continuous here (x is discrete) → y-only major gridlines ggplot2::ggplot(mtcars, ggplot2::aes(factor(cyl), mpg)) + ggplot2::geom_boxplot() + gridlines_plus(color = "grey85", linewidth = 1, linetype = "dashed") # Works with derived continuous axes (histogram) ggplot2::ggplot(mtcars, aes(mpg)) + ggplot2::geom_histogram() + gridlines_plus()library(ggplot2) ggplot2::ggplot(iris, ggplot2::aes(Sepal.Length, Petal.Length)) + ggplot2::geom_point() + theme_plus() + #WE DON'T RECOMMEND USING gridlines_plus() WITHOUT ALSO USING theme_plus() gridlines_plus() # Only y is continuous here (x is discrete) → y-only major gridlines ggplot2::ggplot(mtcars, ggplot2::aes(factor(cyl), mpg)) + ggplot2::geom_boxplot() + gridlines_plus(color = "grey85", linewidth = 1, linetype = "dashed") # Works with derived continuous axes (histogram) ggplot2::ggplot(mtcars, aes(mpg)) + ggplot2::geom_histogram() + gridlines_plus()
scale_continuous_plus() is an opinionated wrapper around ggplot2's
continuous x, y, colour, and fill scales. It chooses "pretty" breaks while
gently expanding the scale limits so breaks generally will appear near both
ends of the data range.
scale_continuous_plus( scale = NA, ..., thin.labels = FALSE, pad.labels = "start", target.breaks = 5, buffer_frac = 0.05, split_name = FALSE )scale_continuous_plus( scale = NA, ..., thin.labels = FALSE, pad.labels = "start", target.breaks = 5, buffer_frac = 0.05, split_name = FALSE )
scale |
Character string specifying which scale to modify. Options are
|
... |
Additional arguments passed to the corresponding ggplot2
continuous scale function. Arguments such as |
thin.labels |
Logical. If |
pad.labels |
Character string, either |
target.breaks |
Integer target number of major breaks. This is a target and
not a guarantee because breaks are chosen using a "pretty" break algorithm.
Default is |
buffer_frac |
Numeric fraction of the data span used to decide whether a
break is close enough to each endpoint. Default is |
split_name |
Logical. If |
This is useful because ggplot2's default continuous scales frequently will leave the ends of an axis or colorbar visually unlabeled, making it look as if an endpoint break is missing.
scale_continuous_plus() routes to one of
ggplot2::scale_x_continuous(), ggplot2::scale_y_continuous(),
ggplot2::scale_colour_continuous(), or
ggplot2::scale_fill_continuous() based on scale.
Unlike the ggplot2 defaults, this function intentionally controls breaks
and limits. If either is supplied through ..., it's ignored with a
warning. Transformed scales are also not currently supported; pre-transform
the data or use ggplot2's scale functions directly when a transformed scale is
needed.
User-supplied label vectors are supported, but endpoint-aware breaks can
sometimes create hidden outer breaks. When possible, this function pads label
vectors with blank labels to align them with the computed break vector in length. If
alignment is ambiguous by one label, use pad.labels to choose which side of the input labels vector to
pad.
A ggplot2 continuous scale object.
library(ggplot2) ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point() + scale_continuous_plus(scale = "x") + scale_continuous_plus(scale = "y") ggplot(iris, aes(Sepal.Width, Sepal.Length, fill = Petal.Length)) + geom_point(shape = 21) + scale_continuous_plus(scale = "x") + scale_continuous_plus(scale = "y") + scale_continuous_plus(scale = "fill") ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point() + scale_continuous_plus( scale = "y", name = "Sepal length", labels = LETTERS[1:5], pad.labels = "start" )library(ggplot2) ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point() + scale_continuous_plus(scale = "x") + scale_continuous_plus(scale = "y") ggplot(iris, aes(Sepal.Width, Sepal.Length, fill = Petal.Length)) + geom_point(shape = 21) + scale_continuous_plus(scale = "x") + scale_continuous_plus(scale = "y") + scale_continuous_plus(scale = "fill") ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point() + scale_continuous_plus( scale = "y", name = "Sepal length", labels = LETTERS[1:5], pad.labels = "start" )
scale_focus_plus() is a wrapper for ggplot2's scale_fill/colour_manual
functions that helps a user quickly creates a manual color or fill scale
that highlights one or more focal groups while de-emphasizing all other
groups (via desaturation, by default). This is useful when a graph would
ideally call attention to specific groups without removing the broader
context and transparency provided by the remaining groups. This reduces
cognitive load by allowing non-essential information to "fall into the
background" to be readily ignored, increasing the reading rate, and also
helps to signpost for the reader where the "message" is within the plot.
scale_focus_plus( aes, group_var, focal_groups, diff_nonfocal = FALSE, diff_focal = TRUE, gray_start = 0.35, gray_end = 0.65, focal_start = 0.75, focal_end = 0.25, custom_focal = NULL, custom_nonfocal = NULL, ... )scale_focus_plus( aes, group_var, focal_groups, diff_nonfocal = FALSE, diff_focal = TRUE, gray_start = 0.35, gray_end = 0.65, focal_start = 0.75, focal_end = 0.25, custom_focal = NULL, custom_nonfocal = NULL, ... )
aes |
A character string indicating which aesthetic should receive the
focus scale. Must be one of |
group_var |
A character vector or factor containing the grouping variable
mapped to |
focal_groups |
A character vector giving the group value(s) in the
|
diff_nonfocal |
Logical. If |
diff_focal |
Logical. If |
gray_start, gray_end
|
Numeric values between 0 and 1 controlling the
range of gray values used for non-focal groups when custom_nonfocal is not
NULL. Lower values are darker and higher values are lighter. |
focal_start, focal_end
|
Numeric values between 0 and 1 controlling the
portions of the viridis palette used for focal groups when custom_focal is
not NULL. By default, |
custom_focal |
Optional custom color(s) for focal group(s). When
|
custom_nonfocal |
Optional custom color(s) for non-focal group(s). When
|
... |
Additional arguments passed to
|
By default, focal groups are assigned visually prominent colors from the
Universal-Design-oriented viridis color palette via viridisLite::viridis(),
while non-focal groups are assigned a shared gray. Focal groups are
differentiated from one another by default; non-focal groups are not. These
defaults are intended to support a common graph-design pattern:
show all groups, but make the intended comparison obvious.
Note that the default values for gray_start, gray_end, focal_start, and
focal_end intentionally map to different regions of the luminance (light
to dark) scale so as to render the colors still distinguishable in grayscale
by virtue of variance in luminance. This variance should be maintained for
accessibility even if different inputs are provided.
scale_focus_plus() is a convenience wrapper around
ggplot2::scale_colour_manual() and ggplot2::scale_fill_manual(). It does
not alter the data or add any geoms. Instead, it constructs a named vector of
colors from group_var and focal_groups, then passes that vector to the
appropriate ggplot2 manual color/fill scale.
This function is most useful when the focal/non-focal distinction is the
primary visual message, and all non-focal groups may be secondary. When
individual non-focal groups must remain distinguishable, set
diff_nonfocal = TRUE or provide custom_nonfocal.
Although scale_focus_plus() is designed for discrete data, it can also be
used when the data requiring (de-)emphasis is continuous. In those cases,
first create a discrete grouping variable in the data, then map that variable
to colour or fill. For example, a continuous measurement could be
converted to groups such as "High cover" and "Other" before calling
scale_focus_plus(). See the examples section for an example.
A ggplot2 scale object.
lake_dat = data.frame( year = rep(2012:2020, times = 5), lake = rep(paste("Lake", LETTERS[1:5]), each = 9), cpue = c( 10, 12, 13, 14, 16, 18, 19, 21, 23, 18, 17, 16, 16, 15, 14, 13, 13, 12, 8, 9, 11, 13, 16, 20, 24, 27, 30, 22, 21, 21, 20, 19, 18, 18, 17, 17, 12, 13, 13, 15, 15, 16, 17, 18, 20 ) ) ggplot2::ggplot( lake_dat, ggplot2::aes(x = year, y = cpue, colour = lake, group = lake) ) + ggplot2::geom_line(linewidth = 1) + ggplot2::geom_point(size = 2) + scale_focus_plus(aes = "colour", group_var = lake_dat$lake, focal_groups = c("Lake A", "Lake C")) # One can use a shared focal color and a custom non-focal gray. ggplot2::ggplot( lake_dat, ggplot2::aes(x = year, y = cpue, colour = lake, group = lake) ) + ggplot2::geom_line(linewidth = 1) + scale_focus_plus(aes = "colour", group_var = lake_dat$lake, focal_groups = c("Lake A", "Lake C"), diff_focal = FALSE, custom_focal = "#440154", custom_nonfocal = "gray70") cover_dat = data.frame( taxon = c("Native plants", "Starry stonewort", "Eurasian watermilfoil", "Curly-leaf pondweed"), mean_cover = c(62, 28, 18, 11) ) ggplot2::ggplot( cover_dat, ggplot2::aes(x = taxon, y = mean_cover, fill = taxon) ) + ggplot2::geom_col() + scale_focus_plus(aes = "fill", group_var = cover_dat$taxon, focal_groups = "Starry stonewort") + ggplot2::labs(x = NULL, y = "Mean percent cover") # Focal groups can also be created from continuous variables. Here's how:lake_dat = data.frame( year = rep(2012:2020, times = 5), lake = rep(paste("Lake", LETTERS[1:5]), each = 9), cpue = c( 10, 12, 13, 14, 16, 18, 19, 21, 23, 18, 17, 16, 16, 15, 14, 13, 13, 12, 8, 9, 11, 13, 16, 20, 24, 27, 30, 22, 21, 21, 20, 19, 18, 18, 17, 17, 12, 13, 13, 15, 15, 16, 17, 18, 20 ) ) ggplot2::ggplot( lake_dat, ggplot2::aes(x = year, y = cpue, colour = lake, group = lake) ) + ggplot2::geom_line(linewidth = 1) + ggplot2::geom_point(size = 2) + scale_focus_plus(aes = "colour", group_var = lake_dat$lake, focal_groups = c("Lake A", "Lake C")) # One can use a shared focal color and a custom non-focal gray. ggplot2::ggplot( lake_dat, ggplot2::aes(x = year, y = cpue, colour = lake, group = lake) ) + ggplot2::geom_line(linewidth = 1) + scale_focus_plus(aes = "colour", group_var = lake_dat$lake, focal_groups = c("Lake A", "Lake C"), diff_focal = FALSE, custom_focal = "#440154", custom_nonfocal = "gray70") cover_dat = data.frame( taxon = c("Native plants", "Starry stonewort", "Eurasian watermilfoil", "Curly-leaf pondweed"), mean_cover = c(62, 28, 18, 11) ) ggplot2::ggplot( cover_dat, ggplot2::aes(x = taxon, y = mean_cover, fill = taxon) ) + ggplot2::geom_col() + scale_focus_plus(aes = "fill", group_var = cover_dat$taxon, focal_groups = "Starry stonewort") + ggplot2::labs(x = NULL, y = "Mean percent cover") # Focal groups can also be created from continuous variables. Here's how:
theme_plus() returns a ggplot2 theme designed to make
publication-quality, accessible graphs easier to produce. It keeps all of
ggplot2’s normal behaviors (last theme wins; user overrides take precedence),
but bakes in opinionated defaults with Universal Design in mind. A few knobs
let you scale typography/lines, flip the legend layout, and switch the
background color if desired.
theme_plus( ..., legend_pos = "top", base_font_size = 16, base_linewidth = 1.2, base_rectlinewidth = 1.2, line_color = "black", text_color = "black", background_color = "#FFFEFD", palette_discrete = "D", palette_continuous = "E", begin_discrete = 0, end_discrete = 0.72, begin_continuous = 0, end_continuous = 1, export_width = 7.25, export_height = 5.95, override_legend_alphasize = TRUE, enable_coaching = TRUE )theme_plus( ..., legend_pos = "top", base_font_size = 16, base_linewidth = 1.2, base_rectlinewidth = 1.2, line_color = "black", text_color = "black", background_color = "#FFFEFD", palette_discrete = "D", palette_continuous = "E", begin_discrete = 0, end_discrete = 0.72, begin_continuous = 0, end_continuous = 1, export_width = 7.25, export_height = 5.95, override_legend_alphasize = TRUE, enable_coaching = TRUE )
... |
Optional additional theme settings passed to |
legend_pos |
Where to put the legend. |
base_font_size |
Base text size (in points) for most text elements. These
will scale via |
base_linewidth |
Baseline thickness for most line theme elements
(e.g., axis lines and tick marks). Defaults to |
base_rectlinewidth |
Baseline line thickness for most rect theme elements
(e.g., legend frames). Defaults to |
line_color |
Default color for most line elements (axis lines, frames, etc.).
Defaults to |
text_color |
Default color for most text elements. Defaults to |
background_color |
Background fill applied to the panel, plot, legend,
and strip backgrounds. Defaults to a slightly warm white, |
palette_discrete, palette_continuous
|
Default viridis-family color palette codes ("A" through "H") to use for discrete and continuous scales, respectively. |
begin_discrete, end_discrete, begin_continuous, end_continuous
|
Numeric values ranging between 0 and 1 for where to begin drawing colors from a viridis palette for a discrete and continuous color scale, respectively. |
export_width, export_height
|
Length-1 numeric values indicating your intended export (most likely via ggplot2::ggsave()) width and height, respectively. This rescales font and line sizes internally to stay relatively appropriately for your intended export size. |
override_legend_alphasize |
Logical indicating whether, for any fill, color, and/or shape legends, to override the size and/or alpha values to the ggplotplus default values (1 and 5, respectively). Defaults to TRUE. |
enable_coaching |
Logical indicating whether ggplotplus should perform certain automated checks for possible non-Universal graph design and provide coaching messages when these are detected. Defaults to TRUE. |
Internally, text sizes are expressed with rel(), so they scale with
base_font_size. Line/rect line thicknesses start from base_linewidth and
base_rectlinewidth and scale similarly with rel().
The function builds
a base theme, then adds any user overrides via
theme(...), so the user's preferences always take precedence.
One interaction exists between gridlines_plus() and theme_plus(): If using
them in tandem, the linewidth provided to gridlines_plus() will be a
function of the scaling factor generated according to the export_width and
export_height inputs provided to theme_plus(). That is to say that the
final gridline widths may be bigger or small than those specified in
gridlines_plus() as a function of your intended output size.
A ggplot2 theme object to add with +.
ggplot2::theme(), ggplot2::theme_gray(), ggplot2::theme_get()
# Basic use library(ggplot2) ggplot(iris, aes(Sepal.Length, Petal.Length, colour = Species)) + geom_point() + theme_plus() # Prefer the right-side legend and pure white background ggplot(mtcars, aes(wt, mpg)) + geom_point() + theme_plus(legend_pos = "right", background_color = "white") # Scale text up and make lines a bit lighter ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point() + theme_plus(base_font_size = 18, base_linewidth = 1.0) # You can still override any element normally via theme() ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point() + theme_plus() + theme(axis.line = element_line(linewidth = 0.8)) # But you could just as easily do so via theme_plus() ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point() + theme_plus(axis.line = element_line(linewidth = 0.8))# Basic use library(ggplot2) ggplot(iris, aes(Sepal.Length, Petal.Length, colour = Species)) + geom_point() + theme_plus() # Prefer the right-side legend and pure white background ggplot(mtcars, aes(wt, mpg)) + geom_point() + theme_plus(legend_pos = "right", background_color = "white") # Scale text up and make lines a bit lighter ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point() + theme_plus(base_font_size = 18, base_linewidth = 1.0) # You can still override any element normally via theme() ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point() + theme_plus() + theme(axis.line = element_line(linewidth = 0.8)) # But you could just as easily do so via theme_plus() ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point() + theme_plus(axis.line = element_line(linewidth = 0.8))
This function relocates the y axis title of a ggplot to the top, above the y axis line and left-justified to the left edge of the y axis labels, sort of like a plot subtitle. It also orients the text horizontally for space-efficiency and easy reading. This is otherwise difficult to do using ggplot2's default styling tools.
yaxis_title_plus( location = "top", nudgeTopLegendDown = FALSE, nudgeHowMuch = 20, override_legend_alphasize = TRUE, enable_coaching = TRUE )yaxis_title_plus( location = "top", nudgeTopLegendDown = FALSE, nudgeHowMuch = 20, override_legend_alphasize = TRUE, enable_coaching = TRUE )
location |
A length-1 character string matching either "top" or "bottom" for the placement of the new y axis title. Defaults to |
nudgeTopLegendDown |
A length-1 logical indicating whether a top legend (box) (if any) should be moved down to align with the relocated y axis title (where they could clip into each other). Defaults to FALSE. |
nudgeHowMuch |
A length-1 positive integer indicating how much to nudge the top legend (box) (if any) down, if |
override_legend_alphasize |
Logical indicating whether, for any fill, color, and/or shape legends, to override the size and/or alpha values to the ggplotplus default values (1 and 5, respectively). Defaults to TRUE. |
enable_coaching |
Logical indicating whether ggplotplus should perform certain automated checks for possible non-Universal graph design and provide coaching messages when these are detected. Defaults to TRUE. |
An ggplot class object for adding to a plot with +.
#WE DO NOT RECOMMEND USING yaxis_title_plus() WITHOUT theme_plus() ggplot2::ggplot(iris, ggplot2::aes(x=Sepal.Length, y=Petal.Length)) + ggplot2::geom_point() + theme_plus() + yaxis_title_plus()#WE DO NOT RECOMMEND USING yaxis_title_plus() WITHOUT theme_plus() ggplot2::ggplot(iris, ggplot2::aes(x=Sepal.Length, y=Petal.Length)) + ggplot2::geom_point() + theme_plus() + yaxis_title_plus()