Learn R Programming

ggpackets

Overview

Take a look at the ggpackets project page!

Easily build components of ggplots without sacrificing the ease of ggplot’s layer parameters and construction syntax.

Installation

install.packages("ggpackets")

or install the development version

devtools::install_github("dgkf/ggpackets", build_vignettes = TRUE)

Get Involved

There are plenty of ways to help contribute:

  1. File issues!
    Found a bug? Think the syntax looks ugly? Don’t like the name? Tell me! Issues are the best way to start the conversation.

  2. Write documentation!
    More resources always helps. Found a function unintuitive? Example code and improved function descriptors would be helpful. If you use the package and would feel comfortable writing about a topic not yet covered in a vignette, feel free to contribute a new vignette for it.

  3. Write Unit Tests!
    There’s some pretty sophisticated manipulations going on under the hood to make everything as clean as possible, because of that it’s important to make sure everything stays working the way we expect it to. Unit test contributions always welcome!

  4. Contribute Code!
    Last but not least, code contributors are welcome. Reach out and get in touch if you’re passionate about the goal of the project.

Quick Start

Define common ggplot layer sets together into a singled object. Connect all your plots with a single plot component definition and debug one central location. Build beautiful templates and save them once, reuse them easily and without having to abandon the ggplot construction framework.

ggpk_box_and_scatter <- ggpacket() +
  geom_point(position = position_jitter(width = 0.4), alpha = 0.02) + 
  geom_boxplot(outlier.shape = NA, fill = NA, color = 'black') + 
  geom_text(stat = 'summary', vjust = -1, fun.data = function(d) c(
    y = quantile(d, 0.75, names = F) + 1.5 * IQR(d),
    label = length(d)
  )) + 
  theme_linedraw() + 
  scale_color_distiller(palette = "Set1")

Now we can use that template repeatedly with a much simpler ggplot call

ggplot(diamonds, aes(x = cut, y = price, color = carat)) + 
  ggpk_box_and_scatter() + 
  ggtitle('Diamond price distribution by cut')

Handle custom arguments & parameter propegation

In addition to simply wrapping multiple ggplot2 layers, ggpackets can streamline a number of complicated plotting scenarios such as passing arguments to sets of layers, setting default argument values with scoped overrides, routing aesthetic mappings to be reused within specific layers for other aesthetics and scoping data usage over a set of layers.

ggpk_labelled_heatmap <- function(...) {
  ggpacket(...) %+%
    geom_tile(.id = 'tile', color = NA, ...) %+% 
    geom_text(.id = c("text", "text1"), color = "black", vjust = -0.3, 
      fontface = "bold", ...) %+%
    geom_text(.id = c("text", "text2"), 
      aes(label = sprintf("(%.1f)", ..fill..)), 
      color = "black", vjust = 1.1, ...) %+%
    theme_void()
} 

In this function we make use of a number of these specialized behaviors.

  1. .id parameters are set to tag specific layers with an identifier, which can be used to prefix arguments to route them to a subset of the ggpacket layers. Multiple IDs can be used, and arguments will filter down into that layer if they match any of the provided IDs.
  2. Ellipsis are first passed to ggpacket(...), which will pass them on as default values to all ggpacket layers.
  3. Ellipsis are also passed at the tail end of each layer call, allowing arguments to mask default values. The placement of the ellipsis determines whether arguments will override or be overridden by the existing parameters. After expanding the ellipsis, the last instance of each argument is used to build the call.
  4. Aesthetics are rerouted using the specialized ..<aesthetic>.. syntax.
  5. We use %+% instead of the commonly-used + to add layers together, which allows ggpackets to accept non-standard arguments before ggplot sends us warnings about them.
ggplot(as.data.frame(OrchardSprays)) + 
  aes(x = rowpos, y = colpos, label = treatment, fill = decrease) + 
  ggpk_labelled_heatmap(text.color = "white", text2.alpha = 0.5) + 
  ggtitle('Honeybee population decline in repellent trial grid')

Copy Link

Version

Install

install.packages('ggpackets')

Monthly Downloads

239

Version

0.2.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Doug Kelkhoff

Last Published

July 5th, 2025

Functions in ggpackets (0.2.2)

ggpacket_call

The function used when a ggpacket is called as a function
required_aesthetics

Check a ggpacket object for required aesthetic arguments
ggpacket

A container for lazy ggplot layers
self

Returning the calling object from within a function
[[.ggpacket

Subset a ggpacket for a selection of ggcalls
infer_ggcall_id

Convert ggplot geom layers to friendly names
substitute_ggcall_dot_aes

Substitute a ggcall's dot aesthetics with their redirected values
ggpackets-package

ggpackets: Package Plot Layers for Easier Portability and Modularization
[.ggpacket

Subset a ggpacket for a selection of ggcalls
infer_ggcall_name

Label ggcall with function name if it can be deduced
with_ignore_unused_argument

Evaluate a call, ignore base R errors for unused arguments
%+%

Lazy handler for ggplot addition
substitute_quote

Substitute a quoted expression in a given environmment
names,ggpacket-method

Fetch the ids associated with each ggcall
only_formals_and_dots

Filter for only arguments that can be accepted by a given function
update_data

Reduce data parameters, iteratively applying functions or masking
ggpacket_plus_ANY

Swallow calls when a ggpacket is added to any expression
smart_swap_mapping_data

Mimic ggplot2 behavior of intelligently interpretting first layer argument
handle_reset_mapping

Specific handling of ..reset.. aesthetic
update_mapping

Reduce a list of mappings, iteratively routing aesthetics
with_ignore_unknown_params

Evaluate an expression, ignoring warnings about unknown parameters
as.list,ggpacket-method

Convert a ggpacket to a list of ggcalls
expand_dots

Expand dot arguments into named arguments
gg_plus_ggpacket

Add a gg object to a ggpacket object
collapse_data

Collapse data arguments
ggpacket-class

A ggpacket object
collapse_mappings

Collapse aesthetic mappings arguments
length,ggpacket-method

Get the number of ggcalls within a ggpacket
match_unnamed_args

Match unnamed arguments
filter_by_ggcall_ids

Filter a named list by ids
[[,ggpacket-method

Index into a ggpacket object
as_gg_call

Convert an expression into a call as a list of quosure components
deduplicate_params

Remove arguments with duplciated names
[,ggpacket-method

Index into a ggpacket object
.all_aesthetics

Extracted .all_aesthetics from internal ggplot2 with hardcoded fallback