Skip to content

Remove ellipsis from scale constructors #1763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions R/scale-alpha.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#' \code{scale_alpha} is an alias for \code{scale_alpha_continuous} since
#' that is the most common use of alpha, and it saves a bit of typing.
#'
#' @param ... Other arguments passed on to \code{\link{continuous_scale}}
#' or \code{\link{discrete_scale}} as appropriate, to control name, limits,
#' breaks, labels and so forth.
#' @inheritParams continuous_scale
#' @param range range of output alpha values. Should lie between 0 and 1.
#' @export
#' @examples
Expand All @@ -19,17 +17,34 @@
#' (p <- ggplot(mtcars, aes(mpg, cyl)) +
#' geom_point(aes(alpha = factor(cyl))))
#' p + scale_alpha_discrete(range = c(0.4, 0.8))
scale_alpha <- function(..., range = c(0.1, 1)) {
continuous_scale("alpha", "alpha_c", rescale_pal(range), ...)
scale_alpha <- function(name = waiver(), range = c(0.1, 1), breaks = waiver(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we wrote forward_to so that this would work:

scale_alpha <- forward_to(continuous_scale, 
  aesthetics = "alpha",
  scale_name = "alpha_c",
  palette = rescale_pal()
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One downside is that this would require continuous_scale to be defined before scale_alpha in order to inspect the arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or what about:

forward_to(contiunous_scale,
  aesthetics = "alpha",
  scale_name = "alpha_c",
  palette = rescale_pal(range),
  range = NULL
)

Copy link
Member

@hadley hadley Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forward_to <- function(fun, ...) {
  call <- as.call(c(quote(fun), find_args(...)))
  eval(call)
}

minor_breaks = waiver(), labels = waiver(),
limits = NULL, oob = censor, expand = waiver(),
na.value = NA_real_, trans = "identity",
guide = "legend") {
args <- as.list(environment())
args$aesthetics <- "alpha"
args$scale_name <- "alpha_c"
args$palette <- rescale_pal(range)
args$range <- NULL
do.call(continuous_scale, args)
}

#' @rdname scale_alpha
#' @export
scale_alpha_continuous <- scale_alpha

#' @rdname scale_alpha
#' @inheritParams discrete_scale
#' @export
scale_alpha_discrete <- function(..., range = c(0.1, 1)) {
discrete_scale("alpha", "alpha_d",
function(n) seq(range[1], range[2], length.out = n), ...)
scale_alpha_discrete <- function(name = waiver(), range = c(0.1, 1),
breaks = waiver(), labels = waiver(),
limits = NULL, expand = waiver(),
na.value = NA, drop = TRUE, guide = "legend") {
args <- as.list(environment())
args$aesthetics <- "alpha"
args$scale_name <- "alpha_d"
args$palette <- function(n) seq(range[1], range[2], length.out = n)
args$range <- NULL
do.call(discrete_scale, args)
}
77 changes: 71 additions & 6 deletions man/scale_alpha.Rd

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