plus-.gg: Add components to a plot or apply function

Description Usage Arguments Details Examples

Description

This is the original ggplot2:::‘+.gg' function with a twist, it’s possible to add functions to a plot object and this function will be applied on it, pretty much like a pipe.

Usage

1
2
## S3 method for class 'gg'
e1 + e2

Arguments

e1

An object of class ggplot() or a theme()

e2

A plot component, as described in ?ggplot2:::`+.gg` or a function

Details

One difference with the behavior of the pipe is that e2 takes only one input, see examples below for many ways to get around this.

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
# draw an intermediate plot
ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_point() +
  plot +
  aes(color = Species) +
  theme_minimal()

# Create an interractive chart with `plotly::ggplotly` conveniently
if(require(plotly))
  ggplot(iris, aes(Petal.Length)) +
    geom_density() +
    ggplotly

# Use a magrittr functional chain, `purrr::partial` or `functional::Curry`
# for multi argument functions
if(require(magrittr))
  ggplot(iris, aes(Sepal.Length)) +
    geom_histogram() +
    (. %>% saveRDS("my_histogram.RDS"))

if(require(purrr))
  ggplot(iris, aes(Sepal.Length)) +
    geom_histogram() +
    partial(saveRDS, file = "my_histogram.RDS")

if(require(functional))
  ggplot(iris, aes(Sepal.Length)) +
    geom_histogram() +
    Curry(saveRDS, file = "my_histogram.RDS")

# Or a bit more flexible to devise your own functions on the fly, but work
# just as well for multi arguments, use
# `rlang::as_function` or `pryr::f` to define a function on the fly
if(require(rlang))
  ggplot(iris, aes(Sepal.Length)) +
    geom_histogram() +
    as_function(~saveRDS(.,"my_histogram.RDS"))

if(require(pryr))
  ggplot(iris, aes(Sepal.Length)) +
    geom_histogram() +
    f(saveRDS(x,"my_histogram.RDS"))

moodymudskipper/ggfun documentation built on May 26, 2019, 3:33 p.m.