Learn R Programming

ggloop

Create ggplot2 plots in a loop.

Overview

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.

Installation

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

Where can I learn about how to use ggloop?

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

The functions

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

How to use 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

Copy Link

Version

Install

install.packages('ggloop')

Monthly Downloads

14

Version

0.1.0

License

GPL-2

Issues

Pull Requests

Stars

Forks

Maintainer

Luke Smith

Last Published

October 20th, 2016

Functions in ggloop (0.1.0)

aes_loop

Create a list of grouped aesthetic mappings.
%M%

The modified combination of the modulus function (%%) and integer divisor function (%/%).
ggloop

Create ggplot plots in a loop.
%L+%

Add components to a ggloop object.
aes_group

Create unique pairings between x, y and dots.
aes_eval

Assign inputs to x, y or dots.
fun.par

Regular expression pattern for determing if possible function parenthesis are present. Searches for "(" and ")" preceeded by any number of characters.
list.pos

Finds the position of a named list element within a list (with no recurssion).
name_subgroups

Extract names for the second level of list components for the returned value of ggloop().
map_aes

Loop through a list of grouped variables and assign class "uneval" to each element in the group.
recycle.NA

A vector recycler using NA.
is.c

Determine if the first element of a parse tree is identical to the c function.
remap_xy_TRUE

Uses expand.grid() to create all possible combinations of xy pairings.
what

Console function for determing: class, type, mode, and names of an object.
remap_xy_NA

Attaches NA during recycling of the smaller of the two vectors.
name_groups

Extract names for the first level of list components for the returned value of ggloop().
messy_eval

Reduce the amount of code by turning this sequence into a function.
is.fun

Is it a function?
rm.gg2

Remove ggplot2 style and stand-alone aesthetic arguments (i.e. y, x:z, etc).
cur_vars_env

Helper functions to select NSE (non-standard evaluation) variable names.
is.op

Determine if an input uses an arithmetical operator (/, +, -, *, ^).
remap_xy_FALSE

Mimicks R's internal recycling mechanism for the shorter of the two vectors.
isFALSE

This is an abbreviation of identical(FALSE, x) to go along with isTRUE()
recycle.vector

A vector recycler using the contents of the shorter vector to do the recycling.
extract

Extract the nth element from vectors in a list.
expand.grid2

A new version of an old favorite with some extra options
%R%

The replacement operator. Replaces the lhs with rhs on the condition that length(lhs) == FALSE (the length is 0).