Skip to content

Commit

Permalink
updated ggcall & quietly require hrbrthemes and ggthemes
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Jan 26, 2020
1 parent 381d60a commit 1a32c81
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 89 deletions.
8 changes: 4 additions & 4 deletions R/default-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ default_pals <- function() {
)
)

if (requireNamespace("hrbrthemes")) {
if (requireNamespace("hrbrthemes", quietly = TRUE)) {
pals$choices$hrbrthemes = list(
"ipsum" = hrbrthemes::ipsum_pal()(9),
"ft" = hrbrthemes::ft_pal()(9)
Expand All @@ -83,7 +83,7 @@ default_themes <- function() {
themes <- list(
ggplot2 = ggplot2
)
if (requireNamespace("ggthemes")) {
if (requireNamespace("ggthemes", quietly = TRUE)) {
ggthemes <- c(
"base", "calc", "economist", "economist_white",
"excel", "few", "fivethirtyeight", "foundation",
Expand All @@ -98,7 +98,7 @@ default_themes <- function() {
}
}

if (requireNamespace("hrbrthemes")) {
if (requireNamespace("hrbrthemes", quietly = TRUE)) {
hrbrthemes <- c(
"ft_rc", "ipsum", "ipsum_ps", "ipsum_rc", "ipsum_tw", "modern_rc"
)
Expand Down Expand Up @@ -141,7 +141,7 @@ default_cols <- function() {
# "Paired" = brewer_pal(palette = "Paired")(12)
)

if (requireNamespace("hrbrthemes")) {
if (requireNamespace("hrbrthemes", quietly = TRUE)) {
cols$ipsum <- hrbrthemes::ipsum_pal()(9)
cols$ipsum <- hrbrthemes::ft_pal()(9)
}
Expand Down
83 changes: 17 additions & 66 deletions R/ggcall.R
Original file line number Diff line number Diff line change
@@ -1,78 +1,28 @@

#' Generate code to create a `ggplot`
#' Generate code to create a \code{ggplot2}
#'
#' @param data Character. Name of the \code{data.frame}.
#' @param mapping List. Named list of aesthetics.
#' @param geom Character. Name of the geom to use (without "geom_").
#' @param geom Character. Name of the geom to use (with or without "geom_").
#' @param geom_args List. Arguments to use in the geom.
#' @param scales Character vector. Scale(s) to use (without "scale_").
#' @param scales Character vector. Scale(s) to use (with or without "scale_").
#' @param scales_args List. Arguments to use in scale(s),
#' if \code{scales} is length > 1, must be a named list with \code{scales} names.
#' @param coord Character. Coordinates to use (without "coord_").
#' @param coord Character. Coordinates to use (with or without "coord_").
#' @param labs List. Named list of labels to use for title, subtitle, x & y axis, legends.
#' @param theme Character. Name of the theme to use (without "theme_").
#' @param theme_args List. Named list for theme arguments.
#' @param facet Character vector. Names of variables to use as facet.
#' @param facet_args List. Named list for facet arguments.
#' @param theme Character. Name of the theme to use (with or without "theme_").
#' @param theme_args Named list. Arguments for \code{\link[ggplot2:theme]{theme}}.
#' @param facet Character vector. Names of variables to use in \code{\link[ggplot2]{facet_wrap}}.
#' @param facet_args Named list. Arguments for \code{\link[ggplot2:facet_wrap]{facet_wrap}}.
#'
#' @return a call
#' @return a \code{call} that can be evaluated with \code{eval}.
#' @export
#'
#' @importFrom stats setNames
#' @importFrom rlang sym syms expr as_name is_call call2
#' @importFrom ggplot2 ggplot aes theme facet_wrap vars coord_flip labs
#'
#' @examples
#' # Default:
#' ggcall()
#'
#' # With data and aes
#' ggcall("mtcars", list(x = "mpg", y = "wt"))
#'
#' # Evaluate the call
#' library(ggplot2)
#' eval(ggcall("mtcars", list(x = "mpg", y = "wt")))
#'
#'
#' # With a geom:
#' ggcall(
#' data = "mtcars",
#' mapping = list(x = "mpg", y = "wt"),
#' geom = "point"
#' )
#'
#' # With options
#' ggcall(
#' data = "mtcars",
#' mapping = list(x = "hp", y = "cyl", fill = "color"),
#' geom = "bar",
#' coord = "flip",
#' labs = list(title = "My title"),
#' theme = "minimal",
#' facet = c("gear", "carb"),
#' theme_args = list(legend.position = "bottom")
#' )
#'
#' # One scale
#' ggcall(
#' data = "mtcars",
#' mapping = list(x = "mpg", y = "wt", color = "qsec"),
#' geom = "point",
#' scales = "color_distiller",
#' scales_args = list(palette = "Blues")
#' )
#'
#' # Two scales
#' ggcall(
#' data = "mtcars",
#' mapping = list(x = "mpg", y = "wt", color = "qsec", size = "qsec"),
#' geom = "point",
#' scales = c("color_distiller", "size_continuous"),
#' scales_args = list(
#' color_distiller = list(palette = "Greens"),
#' size_continuous = list(range = c(1, 20))
#' )
#' )
#' @example examples/ex-ggcall.R
ggcall <- function(data = NULL,
mapping = NULL,
geom = NULL,
Expand Down Expand Up @@ -109,15 +59,16 @@ ggcall <- function(data = NULL,
geom_args <- setNames(list(geom_args), geom)
for (g in geom) {
g_args <- dropNulls(geom_args[[g]])
geom <- expr((!!sym(paste0("geom_", g)))(!!!g_args))
if (!grepl("^geom_", g))
g <- paste0("geom_", g)
geom <- call2(g, !!!g_args)
ggcall <- expr(!!ggcall + !!geom)
}
if (!is.null(scales)) {
if (length(scales) == 1 && !isTRUE(grepl(scales, names(scales_args))))
scales_args <- setNames(list(scales_args), scales)
for (s in scales) {
s_args <- dropNulls(scales_args[[s]])
# scales <- expr((!!sym(paste0("scale_", s)))(!!!s_args))
if (grepl("::", x = s)) {
scl <- strsplit(x = s, split = "::")[[1]]
scl <- call2(scl[2], !!!s_args, .ns = scl[1])
Expand All @@ -135,7 +86,9 @@ ggcall <- function(data = NULL,
ggcall <- expr(!!ggcall + !!labs)
}
if (!is.null(coord)) {
coord <- expr((!!sym(paste0("coord_", coord)))())
if (!grepl("^coord_", coord))
coord <- paste0("coord_", coord)
coord <- call2(coord)
ggcall <- expr(!!ggcall + !!coord)
}
if (!is.null(theme)) {
Expand All @@ -147,16 +100,14 @@ ggcall <- function(data = NULL,
theme <- paste0("theme_", theme)
theme <- call2(theme)
}
theme <- expr(!!theme)
# theme <- expr((!!sym(paste0("theme_", theme)))())
ggcall <- expr(!!ggcall + !!theme)
}
if (!any(c("fill", "color", "size") %in% names(mapping))) {
theme_args$legend.position <- NULL
}
theme_args <- dropNullsOrEmpty(theme_args)
if (length(theme_args) > 0) {
theme_args <- expr(theme(!!!theme_args))
theme_args <- call2("theme", !!!theme_args)
ggcall <- expr(!!ggcall + !!theme_args)
}
if (!is.null(facet)) {
Expand Down
67 changes: 67 additions & 0 deletions examples/ex-ggcall.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Default:
ggcall()

# With data and aes
ggcall("mtcars", list(x = "mpg", y = "wt"))

# Evaluate the call
library(ggplot2)
eval(ggcall("mtcars", list(x = "mpg", y = "wt")))


# With a geom:
ggcall(
data = "mtcars",
mapping = list(x = "mpg", y = "wt"),
geom = "point"
)

# With options
ggcall(
data = "mtcars",
mapping = list(x = "hp", y = "cyl", fill = "color"),
geom = "bar",
coord = "flip",
labs = list(title = "My title"),
theme = "minimal",
facet = c("gear", "carb"),
theme_args = list(legend.position = "bottom")
)

# Theme
ggcall(
"mtcars", list(x = "mpg", y = "wt"),
theme = "theme_minimal",
theme_args = list(
panel.ontop = TRUE,
legend.title = rlang::expr(element_text(face = "bold"))
)
)

# Theme from other package than ggplot2
ggcall(
"mtcars", list(x = "mpg", y = "wt"),
theme = "ggthemes::theme_economist"
)


# One scale
ggcall(
data = "mtcars",
mapping = list(x = "mpg", y = "wt", color = "qsec"),
geom = "point",
scales = "color_distiller",
scales_args = list(palette = "Blues")
)

# Two scales
ggcall(
data = "mtcars",
mapping = list(x = "mpg", y = "wt", color = "qsec", size = "qsec"),
geom = "point",
scales = c("color_distiller", "size_continuous"),
scales_args = list(
color_distiller = list(palette = "Greens"),
size_continuous = list(range = c(1, 20))
)
)
55 changes: 36 additions & 19 deletions man/ggcall.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1a32c81

Please sign in to comment.