Title: | Package Plot Layers for Easier Portability and Modularization |
Version: | 0.2.2 |
Description: | Create groups of 'ggplot2' layers that can be easily migrated from one plot to another, reducing redundant code and improving the ability to format many plots that draw from the same source 'ggpacket' layers. |
Depends: | R (≥ 3.1.0), ggplot2 |
Imports: | utils, methods, rlang |
Suggests: | testthat, dplyr, backports, knitr, rmarkdown, stringi, covr |
License: | MIT + file LICENSE |
RoxygenNote: | 7.3.2 |
Encoding: | UTF-8 |
VignetteBuilder: | knitr |
URL: | https://github.com/dgkf/ggpackets, https://dgkf.github.io/ggpackets/ |
BugReports: | https://github.com/dgkf/ggpackets/issues |
NeedsCompilation: | no |
Packaged: | 2025-07-05 17:31:42 UTC; doug |
Author: | Doug Kelkhoff [aut, cre] |
Maintainer: | Doug Kelkhoff <doug.kelkhoff@gmail.com> |
Repository: | CRAN |
Date/Publication: | 2025-07-05 18:40:02 UTC |
ggpackets: Package Plot Layers for Easier Portability and Modularization
Description
Create groups of ggplot layers that can be easily migrated from one plot to another, reducing redundant code and improving the ability to format many plots that draw from the same source ggpacket layers.
Author(s)
Maintainer: Doug Kelkhoff doug.kelkhoff@gmail.com
See Also
Useful links:
Report bugs at https://github.com/dgkf/ggpackets/issues
Examples
library(ggplot2)
# Prep a tidy data.frame to plot with
airquality_long <- rbind(
data.frame(
airquality[,c("Month", "Day")],
Variable = "Temp",
Measurement = airquality$Temp),
data.frame(
airquality[,c("Month", "Day")],
Variable = "Ozone",
Measurement = airquality$Ozone))
# Build our new composite ggplot geom
geom_error_line <- ggpacket() %+%
geom_ribbon(
.id = "box",
mapping = aes(fill = ..color..),
stat = 'summary',
fun = mean,
fun.min = ~mean(.) - sd(.),
fun.max = ~mean(.) + sd(.),
alpha = 0.15,
color = NA) %+%
geom_line(
.id = "line",
stat = 'summary',
fun = mean,
alpha = 0.8)
# Try it out!
ggplot(airquality_long) +
aes(x = round((Month * 30 + Day) / 4), y = Measurement, color = Variable) +
geom_error_line(na.rm = TRUE, line.size = 1.5) +
scale_y_log10() +
labs(x = "Week")
# for further examples, check out the vignettes
vignette(package = "ggpackets")
Lazy handler for ggplot addition
Description
Lazy handler for ggplot addition
Usage
e1 %+% e2
Arguments
e1 |
Addition lhs. |
e2 |
Addition rhs. |
Value
A new ggpacket
object with e2
appended as an additional
layer or layers.
Extracted .all_aesthetics from internal ggplot2 with hardcoded fallback
Description
Extracted .all_aesthetics from internal ggplot2 with hardcoded fallback
Usage
.all_aesthetics()
Index into a ggpacket object
Description
Index into a ggpacket object
Usage
## S4 method for signature 'ggpacket'
x[i, j, ..., drop = TRUE]
Arguments
x |
A |
i |
A |
j |
Unused. |
... |
Unused. |
drop |
Unused. |
Subset a ggpacket for a selection of ggcalls
Description
Subset a ggpacket for a selection of ggcalls
Usage
## S3 method for class 'ggpacket'
x[i, ...]
Arguments
x |
A ggpacket to subset. |
i |
A vector upon which to subset the ggpacket ggcalls. |
... |
Additional arguments unused. |
Index into a ggpacket object
Description
Index into a ggpacket object
Usage
## S4 method for signature 'ggpacket'
x[[i, j, ...]]
Arguments
x |
A |
i |
A |
j |
Unused. |
... |
Unused. |
Subset a ggpacket for a selection of ggcalls
Description
Subset a ggpacket for a selection of ggcalls
Usage
## S3 method for class 'ggpacket'
x[[i, ...]]
Arguments
x |
A ggpacket to subset. |
i |
A vector upon which to subset the ggpacket ggcalls. |
... |
Additional arguments unused. |
Convert a ggpacket to a list of ggcalls
Description
Convert a ggpacket to a list of ggcalls
Usage
## S4 method for signature 'ggpacket'
as.list(x, ...)
Arguments
x |
A |
... |
Additional arguments unused |
Convert an expression into a call as a list of quosure components
Description
Convert an expression into a call as a list of quosure components
Usage
as_gg_call(x, which = 3L)
Arguments
x |
An expression to convert to a ggcall. |
which |
The relative frame offset in which the expression should be eventually evaluated. |
Collapse data arguments
Description
Reduce data arguments using update_data
Usage
collapse_data(args)
Arguments
args |
A list of arguments. If multiple elements are named "data" they will be collapsed into a single mapping argument, appropriately updating datasets and applying functions as needed. |
Collapse aesthetic mappings arguments
Description
Reduce aesthetic mapping arguments using update_mapping
Usage
collapse_mappings(args)
Arguments
args |
A list of arguments. If multiple elements are named "mapping" they will be collapsed into a single mapping argument, appropriately updating aesthetic mappings. |
Remove arguments with duplciated names
Description
Remove arguments with duplciated names
Usage
deduplicate_params(args)
Arguments
args |
A list of arguments to deduplicate. |
Expand dot arguments into named arguments
Description
Expand dot arguments into named arguments
Usage
expand_dots(expr, envir = parent.frame(2L))
Arguments
expr |
An expression whose arguments need to be expanded. |
envir |
An environment in which to evaluate expansion. |
Filter a named list by ids
Description
Filter a named list (often an aesthetic mapping or argument list) for only
unprefixed elements, elements which don't match any of all_ids
and
those which match one of call_ids
. For those that match, remove the
id prefix.
Usage
filter_by_ggcall_ids(x, call_ids, all_ids)
Arguments
x |
A named list to filter. |
call_ids |
A character vector of valid ids. |
all_ids |
A character vector of all possible ids. |
Add a gg object to a ggpacket object
Description
Add a gg object to a ggpacket object
Usage
gg_plus_ggpacket(e1, e2)
Arguments
e1 |
A ggplot ggproto object. |
e2 |
A ggpacket object. |
A container for lazy ggplot layers
Description
ggpacket
s present a mechanism for easily collecting loose ggplot
layers into objects that can be reused across multiple plots, as well as
handling of hierarchical argument propegation, as well as data and aesthetic
scoping.
Usage
ggpacket(...)
Arguments
... |
Arguments passed on to
|
Details
The ggpacket
object behaves like a function, returning an updated
ggpacket
. As well, it contains a few slots which can be used for
programmatically accessing the lazy ggplot calls.
Within ggpacket
and subsequent ggplot
layer calls, aesthetic
mappings can contain references to previously mapped aesthetics using the
double-dot keywords (e.g. ..x..
). In addition, the keyword
..reset..
can be used to revert aesthetics within the ggpacket
or layer to an un-specified state.
Because ggpacket
s will tolerate a more flexible syntax for layer
specifications, it's preferrable to use the ggplot
composition
operator %+%
(instead of +
). This allows for duplicate
argument names and non-standard aesthetics to be passed, which are both
handled internally within the ggpacket
call, but will trigger
warnings when using a bare +
.
Value
A new ggpacket
object
Slots
data
A lazy reference to the
data
parameter of theggpacket
, allowing for scoping the data used by a block ofggplot
layers.mapping
A lazy reference to the
mapping
parameter of theggpacket
, allowing for scoping of aesthetic mappings over a block ofggplot
layers.dots
Quosures representing arguments to be passed to all of the
ggpacket
'sggplot
layers.ggcalls
A list containing the layers stored within the
ggpacket
Examples
library(ggplot2)
# create a ggpacket directly, setting some fixed argument settings
ggpk_simple <- ggpacket() %+% geom_line(color = "red") %+% geom_point()
ggplot(mtcars, aes(x = wt, y = mpg)) + ggpk_simple()
# any non-fixed arguments can be modified in the ggpacket call
ggplot(mtcars, aes(x = wt, y = mpg)) + ggpk_simple(color = "green")
# arguments can be specified for only specific layers by prefixing them
ggplot(mtcars, aes(x = wt, y = mpg)) + ggpk_simple(point.size = 5)
# allow masking of preset arguments by wrapping the ggpacket in a function
ggpk_func <- function(...) {
ggpacket() %+%
geom_line(...) %+%
geom_point(color = "red", ...)
}
ggplot(mtcars, aes(x = wt, y = mpg)) +
ggpk_func(color = "purple", line.linewidth = 2, point.size = 4)
A ggpacket object
Description
A ggpacket object
Add a ggpacket object to another, arbitrary object
Display contents of a ggpacket
Usage
## S4 method for signature 'ggpacket,ANY'
e1 + e2
## S4 method for signature 'ggpacket'
show(object)
Arguments
e1 |
A |
e2 |
Any object. |
object |
A ggpacket object to show. |
Slots
data
A dataset or waiver to use within the ggpacket ggcalls.
mapping
A ggplot aesthetic mapping to use within the ggpacket ggcalls.
dots
Arguments which should be passed before prespecified arguments to each ggcall.
ggcalls
A list of lazily evaluated ggplot layer construction expressions.
The function used when a ggpacket
is called as a function
Description
The function used when a ggpacket
is called as a function
Usage
ggpacket_call(mapping = NULL, data = NULL, ..., .id = character(0L))
Arguments
mapping |
The aesthetic mapping to use as a basis for all layers within
the |
data |
The data argument to use for all layers within the
|
... |
additional arguments passed to all bundled |
.id |
an optional identifier tag for the |
Value
A new ggpacket
object with the new defaults applied
Swallow calls when a ggpacket is added to any expression
Description
Swallow calls when a ggpacket is added to any expression
Usage
ggpacket_plus_ANY(e1, e2)
Arguments
e1 |
A ggpacket object. |
e2 |
Any object. |
Specific handling of ..reset.. aesthetic
Description
Specific handling of ..reset.. aesthetic
Usage
handle_reset_mapping(mapping)
Arguments
mapping |
A ggplot2 aesthetic mapping. |
Convert ggplot geom layers to friendly names
Description
Convert ggplot geom layers to friendly names
Usage
infer_ggcall_id(x)
Arguments
x |
A function name from which an id should be inferred. |
Label ggcall with function name if it can be deduced
Description
Label ggcall with function name if it can be deduced
Usage
infer_ggcall_name(expr)
Arguments
expr |
An expression from which a call name should be inferred. |
Get the number of ggcalls within a ggpacket
Description
Get the number of ggcalls within a ggpacket
Usage
## S4 method for signature 'ggpacket'
length(x)
Arguments
x |
A |
Match unnamed arguments
Description
Similar to match.call, but without evaluating arguments, avoiding possible syntactic errors that would arise due to ggpackets-specific syntax.
Usage
match_unnamed_args(f, args, envir = parent.frame())
Arguments
f |
A function to match arguments against. |
args |
A list of arguments to match. |
envir |
An environment in which the function should be matched. |
Fetch the ids associated with each ggcall
Description
Fetch the ids associated with each ggcall
Usage
## S4 method for signature 'ggpacket'
names(x)
Arguments
x |
A |
Filter for only arguments that can be accepted by a given function
Description
Filter for only arguments that can be accepted by a given function
Usage
only_formals_and_dots(f, args)
Arguments
f |
A function to filter on. |
args |
A list of arguments to filter. |
Check a ggpacket object for required aesthetic arguments
Description
Check a ggpacket object for required aesthetic arguments
Usage
required_aesthetics(x)
Arguments
x |
A |
Returning the calling object from within a function
Description
Used for retrieving an S4 object being called as though it is a function
Usage
self(which = -1L)
Arguments
which |
A relative environment offset in which to search for an object with a name of the calling expression. |
Mimic ggplot2 behavior of intelligently interpretting first layer argument
Description
Like ggplot, if the first argument doesn't appear to be an aesthetic mapping, despite mappings being appropriately passed in the first argument, swap the first two arguments.
Usage
smart_swap_mapping_data(args)
Arguments
args |
a list of arguments to interpet |
Substitute a ggcall's dot aesthetics with their redirected values
Description
Substitute a ggcall's dot aesthetics with their redirected values
Usage
substitute_ggcall_dot_aes(mapping, ggcall, envir = parent.frame())
Arguments
mapping |
A ggplot2 aesthetic mapping. |
ggcall |
A ggcall list of expressions. |
envir |
An environment in which the dot aesthetics should be evaluated. |
Substitute a quoted expression in a given environmment
Description
Substitute a quoted expression in a given environmment
Usage
substitute_quote(q, env = parent.frame())
Arguments
q |
A quote to evaluate. |
env |
An environment in which the quote should be evaluated. |
Reduce data parameters, iteratively applying functions or masking
Description
Reduce data parameters, iteratively applying functions or masking
Usage
update_data(d1, d2, ...)
Arguments
d1 |
A plot data object to update |
d2 |
A second plot data object with which to update |
... |
Additional objects to sequentially collapse. |
Reduce a list of mappings, iteratively routing aesthetics
Description
Reduce a list of mappings, iteratively routing aesthetics
Usage
update_mapping(...)
Arguments
... |
A series of mappings to be sequentially collapsed |
Evaluate an expression, ignoring warnings about unknown parameters
Description
Evaluate an expression, ignoring warnings about unknown parameters
Usage
with_ignore_unknown_params(expr, envir = parent.frame())
Arguments
expr |
An expression to evaluate. |
envir |
An environment to evaluate the given expression in. |
Evaluate a call, ignore base R errors for unused arguments
Description
In actuality, the expression is repeatedly called while catching errors and removing error-causing arguments.
Usage
with_ignore_unused_argument(call, envir = parent.frame())
Arguments
call |
A call to evaluate. |
envir |
An environment to evaluate the given expression in. |