Intro to ggloop"

Table of Contents


ggloop's main function is ggloop(), and it allows the user to create one of two things:

  1. Multiple ggplot2 plots.
  2. A list of aesthetics, which can used to make ggplot2 plots using ggplot().

Instead of passing a single aesthetic (x, y, ...) to ggplot(), the user can pass multiple aesthetics by using a vector of aesthetics. "Remapping"" behaviors control how the x and y vectors are paired with one another, and they control how mutliple ... arguments are paired with one another.

The gg_obs argument controls the returned value: either ggplot2 plots or a list of aesthetics which can be used to make ggplot2 plots. gg_obs's default value is TRUE, which instructs ggloop() to return plots. FALSE instructs ggloop() to return a list of aesthetics.

Main Functions


Mimicry of ggplot()'s arguments (for the most part).



There are two main types of returned values when gg_obs = TRUE:

  1. A list of ggplot2 plots - when no ... are specified in aes_loop().
  2. A nested list (a list of a list) of ggplot2 plots - when there are ... specified in aes_loop().


Mimicry of ggplot()'s aes(), except:

  1. You can use dplyr's syntax to call variables (i.e. y = mpg:cyl, x = 1:5, etc). You can still use ggplot2's syntax to call variables (i.e. x = disp, x = mpg/hp, color = factor(cyl), y = gear + cyl, etc). You can also use both within a vector, but do adhere to the rule below.
  2. If you are calling more than one variable, then you need to wrap the variables in c() and not have any other c() within that wrapping (i.e. x = c(mpg:cyl, 5, 6:8, gear + cyl, mpg/hp), color = c(factor(cyl), factor(gear))).



Mimicry of + with magrittr-like implementation. Can be used to add to three different types of objects:

  1. A single ggplot2 plot.
  2. A list of ggplot2 plots.
  3. A nested list (a list of a list) of ggplot2 plots.

The user thus has the ability to add components to a subset of plots. Much like +, the results of %L+% will not be saved unless they are assigned.

A Trivial Example

Example 1

Example: Using the mtcars data set, create and plot all possible xy-pairs between mpg and hp.
Steps: Load the necessary packages, create plots with ggloop(), and add to plots using %L+%.

g <- ggloop(mtcars, aes_loop(x = mpg:hp, y = mpg:hp))

View some of the plots.

g$x.mpg_y.hp %L+% geom_point()
g$x.cyl_y.disp %L+% geom_point()
g$x.disp_y.hp %L+% geom_point() %L+% geom_line()

Use %L+% to add to two plots, save the output, and then display the results.

g[1:2] <- g[1:2] %L+% geom_point()

Look at the mappings of each name in g. Note how the list is NOT nested (because no ... were called in aes_loop()).

lapply(g, `[[`, "mapping")

Example 2

Example: Plot c(mpg, disp, hp) against wt and facet the resulting plots.
Steps: Create plots, add geoms, and add facet_grid().

g2 <- ggloop(mtcars, aes_loop(x = c(mpg, disp, hp), y = wt, color = factor(cyl))) %L+%
  geom_point() %L+%
  facet_grid(. ~ cyl)

View some plots


Look at the mappings of each name in g2. Note how the list is nested (because a ... argument was called in aes_loop()).

lapply(g2, function(x) lapply(x, `[[`, "mapping"))

Example 3

Example: Return the list of aesthetics rather than the list of plots.
Steps: Call ggloop() with gg_obs = FALSE.

g3 <- ggloop(mtcars,
             aes_loop(x = c(mpg, disp, hp), y = wt, color = c(factor(cyl), factor(gear))), 
             gg_obs = FALSE)

Try the ggloop package in your browser

Any scripts or data that you put into this service are public.

ggloop documentation built on May 2, 2019, 3:01 a.m.