ggloop: Create ggplot plots in a loop.

Description Usage Arguments Details Examples

View source: R/ggloop.R

Description

ggloop() mimics ggplot() by accepting both a data frame and mappings, returning a plot - or plots in this case. The main difference is that ggloop() accepts vectors for aesthetics and returns a list or nested list of ggplot plots.

Usage

1
2
ggloop(data, mappings = aes_loop(), remap_xy = TRUE, remap_dots = FALSE,
  gg_obs = TRUE, ..., environment = parent.frame())

Arguments

data

Default dataset to use for plot. Must be a data frame and can be only one data frame.

mappings

List of aesthetic mappings to use for plots. Works like mapping from ggplot().

remap_xy

Remapping behavior of the x and y vectors specified in aes_loop(). See details below for more on remapping behavior.

remap_dots

Remapping behavior of the ... vectors specified in aes_loop(). See details below for more on remapping behavior.

gg_obs

Logical. Specifies whether to return plots or the list (or nested list) of aesthetics used to make such a plots.

...

Other arguments. Similar to ggplot()'s ....

environment

An environment and only one environment (cannot be a vector). Similar to ggplot()'s environment.

Details

ggloop() makes use of aes_loop, which is meant to mimic aes from ggplot2. Because of this, the remapping arguments are supplied to ggloop instead of aes_loop().

The first remapping argument, remap_xy can take three values:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 1. Return all possible x-y combinations.
plots <- ggloop(data = mtcars,
                mappings = aes_loop(x = mpg:carb, y = mpg:carb))
names(plots)
# [1] "x.mpg_y.cyl"   "x.mpg_y.disp"  "x.mpg_y.hp"    "x.mpg_y.drat"
# [5] "x.mpg_y.wt"    "x.mpg_y.qsec"  "x.mpg_y.vs"    "x.mpg_y.am"
# [9] "x.mpg_y.gear"  "x.mpg_y.carb"  "x.cyl_y.disp"  "x.cyl_y.hp"
# [13] "x.cyl_y.drat"  "x.cyl_y.wt"    "x.cyl_y.qsec"  "x.cyl_y.vs"
# [17] "x.cyl_y.am"    "x.cyl_y.gear"  "x.cyl_y.carb"  "x.disp_y.hp"
# [21] "x.disp_y.drat" "x.disp_y.wt"   "x.disp_y.qsec" "x.disp_y.vs"
# [25] "x.disp_y.am"   "x.disp_y.gear" "x.disp_y.carb" "x.hp_y.drat"
# [29] "x.hp_y.wt"     "x.hp_y.qsec"   "x.hp_y.vs"     "x.hp_y.am"
# [33] "x.hp_y.gear"   "x.hp_y.carb"   "x.drat_y.wt"   "x.drat_y.qsec"
# [37] "x.drat_y.vs"   "x.drat_y.am"   "x.drat_y.gear" "x.drat_y.carb"
# [41] "x.wt_y.qsec"   "x.wt_y.vs"     "x.wt_y.am"     "x.wt_y.gear"
# [45] "x.wt_y.carb"   "x.qsec_y.vs"   "x.qsec_y.am"   "x.qsec_y.gear"
# [49] "x.qsec_y.carb" "x.vs_y.am"     "x.vs_y.gear"   "x.vs_y.carb"
# [53] "x.am_y.gear"   "x.am_y.carb"   "x.gear_y.carb"

plots$x.mpg_y.hp + ggplot2::geom_point()

# 2. Add an additional aesthetic (facet) to plots.
plots2 <- ggloop(data = mtcars,
                 mappings = aes_loop(
                                     x = c(disp, hp, wt),
                                     y = mpg,
                                     color = factor(cyl)))
sapply(plots2, names)
#      color.factor(cyl)
# [1,] "x.disp_y.mpg"
# [2,] "x.hp_y.mpg"
# [3,] "x.wt_y.mpg"

plots2$`color.factor(cyl)`$x.hp_y.mpg + ggplot2::geom_point()

# A look at remap_xy's other two behaviors:
# 3. remap_xy = NA
#    The longer vector will go "unpaired" after the shorter vector
#    runs out of elements.
plots3 <- ggloop(data = mtcars,
                 mappings = aes_loop(x = c(mpg/disp, mpg/hp, mpg/cyl, mpg/gear),
                                     y = c(hp, disp)),
                 remap_xy = NA)
names(plots3)
# [1] "x.mpg/disp_y.hp" "x.mpg/hp_y.disp" "x.mpg/cyl"       "x.mpg/gear"

# 4. remap_xy = FALSE
#    The longer vector will be "paired" with the shorter vector using
#    recycling (similar to R's internal recycling, i.e. mapply()).
plots4 <- ggloop(data = mtcars,
                 mappings = aes_loop(x = c(mpg/disp, mpg/hp, mpg/cyl, mpg/gear),
                                     y = c(hp, disp)),
                 remap_xy = FALSE)
sapply(plots4, names)
# [1] "x.mpg/disp_y.hp"   "x.mpg/hp_y.disp"   "x.mpg/cyl_y.hp"    "x.mpg/gear_y.disp"

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