Create 'ggplot2' Plots in a Loop

Pass a data frame and mapping aesthetics to ggloop() in order to create a list of 'ggplot2' plots. The way x-y and dots are paired together is controlled by the remapping arguments. Geoms, themes, facets, and other features can be added with the special %L+% (L-plus) operator.


ggloop allows the user to use both dplyr-like and ggplot2-like syntax to either create multiple ggplot2 plots or create the list of aesthetics used to make such plots. This has the potential to save the users on the amount of code within their projects or sessions.

Please use devtools::install_github("seasmith/ggloop") to install the package.

You can try the Intro vignette (vignette("intro", "ggloop")). You can also read the very brief overview of the functions and example below.

ggloop has three exported functions:

  • ggloop(data, mappings = aes_loop(), remap_xy = TRUE, remap_dots = FALSE, ..., environment = parent.frame() ) = Meant to mimick ggplot()'s arguments with additional remap arguments to control the remapping behavior of the mappings. There are three possible returned values:
    • A single ggplot object = Created by x, y, and ... arguments of length one or less.
    • A list of ggplot objects = Created when there is no ... argument in aes_loop().
    • A nest list (a list of a list) of ggplot objects = Created when a ... argument is supplied. ... names sit on the top-level of the nested list (they divide the list into however many parts based on the number of such arguments and the remapping behavior). x and y sit at the bottom-level of the nested list
  • aes_loop() = meant to mimick aes(); can accept one or more arguments (a vector of arguments) with dplyr-like and ggplot2-like syntax; both syntax styles can be combined for one argument using the c() as a wrapper and only c().
    • dplyr-like = mpg:hp, 1, 5:9, cyl, etc
    • ggplot2-like = factor(cyl), gear + cyl, etc
  • %L+% = magrittr-like (rip-off) + operator to accomodate the addition of geoms, stats, etc to any of the returned values of ggloop()

A simple example:

g <- ggplot(data = mtcars, 
            mappings = aes_loop(x = c(mpg:hp, mpg/cyl), 
                                y = c(hp:mpg, disp/hp),
                                color = gear), 
            remap_xy = FALSE)
g <- g %L+% geom_point()  ## add a simple point geom to every ggplot object
g$gear$`x.mpg/cyl_y.disp/hp`  ## view one of the plots

News

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("ggloop")

0.1.0 by Luke Smith, 5 months ago


https://github.com/seasmith/ggloop


Report a bug at https://github.com/seasmith/ggloop/issues


Browse source code at https://github.com/cran/ggloop


Authors: Luke Smith [aut, cre]


Documentation:   PDF Manual  


GPL-2 license


Imports plyr, ggplot2, magrittr, lazyeval, assertthat

Suggests testthat, knitr, rmarkdown


See at CRAN