knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

gg fun

Install with:

devtools::install_github("moodymudskipper/ggfun")

This package offers a handy alternative to piped operations on ggplot objects with the additional feature of being able to work directly on the underlying data. All this just by overriding +.gg and defining -.gg to accept functions or formulas (through rlang::as_function) on the right hand side.

When ggfun is attached, ggplot2 will be as well:

library(ggfun)

Apply a function on a ggplot object using +

Draw an intermediate plot

ggplot(iris, aes(Petal.Length, Petal.Width)) +
 geom_point() +
 plot +
 aes(color = Species) +
 theme_minimal()

Use plotly::ggplotly conveniently

if(require(plotly, quietly = TRUE, warn.conflicts = FALSE))
 ggplot(iris, aes(Petal.Length)) +
   geom_density() +
   ggplotly

If you need to provide additional arguments to the function some options are: magrittr functional chains purrr::partial * functional::Curry

if (require(magrittr, quietly = TRUE, warn.conflicts = FALSE))
 ggplot(iris, aes(Sepal.Length)) +
   geom_histogram() +
   (. %>% saveRDS("my_histogram.RDS"))

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

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

If you need more flexibility or want to design a function on the fly, some options are:

These work just as well for the use case presented above.

if(require(rlang, quietly = TRUE, warn.conflicts = FALSE))
 ggplot(iris, aes(Sepal.Length)) +
   geom_histogram() +
   as_function(~saveRDS(.,"my_histogram.RDS"))

if(require(pryr, quietly = TRUE, warn.conflicts = FALSE))
 ggplot(iris, aes(Sepal.Length)) +
   geom_histogram() +
   f(saveRDS(x,"my_histogram.RDS"))

Apply a function on a ggplot object's data using -

Restrict the data to its first 6 rows.

ggplot(iris, aes(Petal.Length)) +
  geom_density() -
  head

Build methods for gg objects using ggmethod

library(ggplot2)
library(dplyr,quietly = TRUE,warn.conflicts = FALSE)

head.gg <- ggmethod(head)

# apply on waiver
ggplot(iris,aes(Sepal.Length, Sepal.Width, color=Species)) +
 geom_point() %>%
 head() +
 theme_minimal()

# apply on data
ggplot(NULL,aes(Sepal.Length, Sepal.Width, color=Species)) +
 geom_point(data=iris) %>%
 head() +
 theme_minimal()

# apply on function
ggplot(iris,aes(Sepal.Length, Sepal.Width, color=Species)) +
 geom_point(data=identity) %>%
 head() +
 theme_minimal()

gg methods for tidyverse verbs

ggfun includes methods for all dplyr and tidyr generics which have a data.frame or tbl_df method.

For instance we can use mutate.gg the following way :

ggplot(iris,aes(Sepal.Length)) +
 geom_point(aes(y=SL2)) %>%
 mutate(SL2 = sin(Sepal.Length), Sepal.Length = Sepal.Length*10) +
 theme_minimal()

Note that this doesn't include all common verbs, for instance *_if and *_at variants are not generic

In some cases it provides an alternative to the idiomatic ggplot2 syntax for users that might be more comfortable with the dplyr interface.

ggplot(iris, aes(Species, Sepal.Width, fill=Species)) +
  stat_summary(fun.y="mean", geom="bar")+
  ggtitle("Idiomatic ggplot2 syntax")

ggplot(iris, aes(Species, Sepal.Width, fill=Species)) +
  geom_col() %>% group_by(Species) %>% summarize(Sepal.Width = mean(Sepal.Width)) +
  ggtitle("Using dplyr verbs")

ggmethod was also used to build the function reorder.gg detailed below.

reorder

reorder is a function from base package stats for which we provide methods for classes data.frame and gg.

It allows easy reordering of factor levels, which is a typical task when working with ggplot2

ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) %>%
  reorder(Species = mean(Sepal.Width)) +
  geom_point() +
  facet_wrap(~Species)

It's the same as using stats::reorder and doing:

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  facet_wrap(vars(Species = reorder(Species,Sepal.Width, mean)))





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