Title: | Package Plot Layers for Easier Portability and Modularization |
---|---|
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. |
Authors: | Doug Kelkhoff [aut, cre] |
Maintainer: | Doug Kelkhoff <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.1 |
Built: | 2024-11-09 04:07:55 UTC |
Source: | https://github.com/dgkf/ggpackets |
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.
Maintainer: Doug Kelkhoff [email protected]
Useful links:
Report bugs at https://github.com/dgkf/ggpackets/issues
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")
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")
Extracted .all_aesthetics from internal ggplot2 with hardcoded fallback
.all_aesthetics()
.all_aesthetics()
Index into a ggpacket object
## S4 method for signature 'ggpacket' x[i, j, ..., drop = TRUE]
## S4 method for signature 'ggpacket' x[i, j, ..., drop = TRUE]
x |
A |
i |
A |
j |
Unused. |
... |
Unused. |
drop |
Unused. |
Subset a ggpacket for a selection of ggcalls
## S3 method for class 'ggpacket' x[i, ...]
## S3 method for class 'ggpacket' x[i, ...]
x |
A ggpacket to subset. |
i |
A vector upon which to subset the ggpacket ggcalls. |
... |
Additional arguments unused. |
Index into a ggpacket object
## S4 method for signature 'ggpacket' x[[i, j, ...]]
## S4 method for signature 'ggpacket' x[[i, j, ...]]
x |
A |
i |
A |
j |
Unused. |
... |
Unused. |
Subset a ggpacket for a selection of ggcalls
## S3 method for class 'ggpacket' x[[i, ...]]
## S3 method for class 'ggpacket' x[[i, ...]]
x |
A ggpacket to subset. |
i |
A vector upon which to subset the ggpacket ggcalls. |
... |
Additional arguments unused. |
Lazy handler for ggplot addition
e1 %+% e2
e1 %+% e2
e1 |
Addition lhs. |
e2 |
Addition rhs. |
A new ggpacket
object with e2
appended as an additional
layer or layers.
Convert a ggpacket to a list of ggcalls
## S4 method for signature 'ggpacket' as.list(x)
## S4 method for signature 'ggpacket' as.list(x)
x |
A |
Reduce data arguments using update_data
collapse_data(args)
collapse_data(args)
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. |
Reduce aesthetic mapping arguments using update_mapping
collapse_mappings(args)
collapse_mappings(args)
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
deduplicate_params(args)
deduplicate_params(args)
args |
A list of arguments to deduplicate. |
Expand dot arguments into named arguments
expand_dots(expr, envir = parent.frame(2L))
expand_dots(expr, envir = parent.frame(2L))
expr |
An expression whose arguments need to be expanded. |
envir |
An environment in which to evaluate expansion. |
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.
filter_by_ggcall_ids(x, call_ids, all_ids)
filter_by_ggcall_ids(x, call_ids, all_ids)
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
gg_plus_ggpacket(e1, e2)
gg_plus_ggpacket(e1, e2)
e1 |
A ggplot ggproto object. |
e2 |
A ggpacket object. |
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.
ggpacket(...)
ggpacket(...)
... |
Arguments passed on to
|
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 +
.
A new ggpacket
object
data
A lazy reference to the data
parameter of the
ggpacket
, allowing for scoping the data used by a block of
ggplot
layers.
mapping
A lazy reference to the mapping
parameter of the
ggpacket
, allowing for scoping of aesthetic mappings over a block of
ggplot
layers.
dots
Quosures representing arguments to be passed to all of the
ggpacket
's ggplot
layers.
ggcalls
A list containing the layers stored within the ggpacket
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", size = 2, point.size = 4)
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", size = 2, point.size = 4)
ggpacket
is called as a functionThe function used when a ggpacket
is called as a function
ggpacket_call(mapping = NULL, data = NULL, ..., .id = character(0L))
ggpacket_call(mapping = NULL, data = NULL, ..., .id = character(0L))
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 |
A new ggpacket
object with the new defaults applied
Swallow calls when a ggpacket is added to any expression
ggpacket_plus_ANY(e1, e2)
ggpacket_plus_ANY(e1, e2)
e1 |
A ggpacket object. |
e2 |
Any object. |
A ggpacket object
Add a ggpacket object to another, arbitrary object
Display contents of a ggpacket
## S4 method for signature 'ggpacket,ANY' e1 + e2 ## S4 method for signature 'ggpacket' show(object)
## S4 method for signature 'ggpacket,ANY' e1 + e2 ## S4 method for signature 'ggpacket' show(object)
e1 |
A |
e2 |
Any object. |
object |
A ggpacket object to show. |
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.
Specific handling of ..reset.. aesthetic
handle_reset_mapping(mapping)
handle_reset_mapping(mapping)
mapping |
A ggplot2 aesthetic mapping. |
Get the number of ggcalls within a ggpacket
## S4 method for signature 'ggpacket' length(x)
## S4 method for signature 'ggpacket' length(x)
x |
A |
Similar to match.call, but without evaluating arguments, avoiding possible syntactic errors that would arise due to ggpackets-specific syntax.
match_unnamed_args(f, args, envir = parent.frame())
match_unnamed_args(f, args, envir = parent.frame())
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
## S4 method for signature 'ggpacket' names(x)
## S4 method for signature 'ggpacket' names(x)
x |
A |
Filter for only arguments that can be accepted by a given function
only_formals_and_dots(f, args)
only_formals_and_dots(f, args)
f |
A function to filter on. |
args |
A list of arguments to filter. |
Check a ggpacket object for required aesthetic arguments
required_aesthetics(x)
required_aesthetics(x)
x |
A |
Used for retrieving an S4 object being called as though it is a function
self(which = -1L)
self(which = -1L)
which |
A relative environment offset in which to search for an object with a name of the calling expression. |
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.
smart_swap_mapping_data(args)
smart_swap_mapping_data(args)
args |
a list of arguments to interpet |
Substitute a ggcall's dot aesthetics with their redirected values
substitute_ggcall_dot_aes(mapping, ggcall, envir = parent.frame())
substitute_ggcall_dot_aes(mapping, ggcall, envir = parent.frame())
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
substitute_quote(q, env = parent.frame())
substitute_quote(q, env = parent.frame())
q |
A quote to evaluate. |
env |
An environment in which the quote should be evaluated. |
Reduce data parameters, iteratively applying functions or masking
update_data(d1, d2, ...)
update_data(d1, d2, ...)
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
update_mapping(...)
update_mapping(...)
... |
A series of mappings to be sequentially collapsed |
Evaluate an expression, ignoring warnings about unknown parameters
with_ignore_unknown_params(expr, envir = parent.frame())
with_ignore_unknown_params(expr, envir = parent.frame())
expr |
An expression to evaluate. |
envir |
An environment to evaluate the given expression in. |