Package 'ggpackets'

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

Help Index


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 [email protected]

See Also

Useful links:

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")

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 ggpacket object.

i

A character or numeric vector for indexing.

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 ggpacket object.

i

A character or numeric value for indexing.

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.


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.


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 ggpacket object


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

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.

Usage

ggpacket(...)

Arguments

...

Arguments passed on to ggpacket_call

data

The data argument to use for all layers within the ggpacket

mapping

The aesthetic mapping to use as a basis for all layers within the ggpacket. Layer-specific mappings will update the value of the mapping, and assigning ..reset.. to any aesthetic field will return the propegated aesthetic mapping to a default, unmapped state for that layer.

.id

an optional identifier tag for the ggpacket, used for filtering arguments and aesthetics that are propegated into the contained ggplot layers.

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 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 +.

Value

A new ggpacket object

Slots

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

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", size = 2, point.size = 4)

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 ggpacket. Layer-specific mappings will update the value of the mapping, and assigning ..reset.. to any aesthetic field will return the propegated aesthetic mapping to a default, unmapped state for that layer.

data

The data argument to use for all layers within the ggpacket

...

additional arguments passed to all bundled ggplot layers, and will be overwritten by layer-specific arguments if provided.

.id

an optional identifier tag for the ggpacket, used for filtering arguments and aesthetics that are propegated into the contained ggplot layers.

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.


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 ggpacket object.

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.


Specific handling of ..reset.. aesthetic

Description

Specific handling of ..reset.. aesthetic

Usage

handle_reset_mapping(mapping)

Arguments

mapping

A ggplot2 aesthetic mapping.


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 ggpacket object


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 ggpacket object


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 ggpacket object or related ggplot component


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 d1

...

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.