| ggpacket | R Documentation |
ggpackets 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(...)
... |
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 ggpackets 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
dataA lazy reference to the data parameter of the
ggpacket, allowing for scoping the data used by a block of
ggplot layers.
mappingA lazy reference to the mapping parameter of the
ggpacket, allowing for scoping of aesthetic mappings over a block of
ggplot layers.
dotsQuosures representing arguments to be passed to all of the
ggpacket's ggplot layers.
ggcallsA 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.