Skip to content

Convert formula to function in sec_axis() #3038

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

Merged
merged 6 commits into from
Apr 13, 2019
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ core developer team.
* `stat_bin()` now handles data with only one unique value (@yutannihilation
#3047).

* `sec_axis()` now accepts functions as well as formulas (@yutannihilation, #3031).

# ggplot2 3.1.0

## Breaking changes
Expand Down
28 changes: 13 additions & 15 deletions R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' secondary axis, positioned opposite of the primary axis. All secondary
#' axes must be based on a one-to-one transformation of the primary axes.
#'
#' @param trans A transformation formula
#' @param trans A formula or function of transformation
#'
#' @param name The name of the secondary axis
#'
Expand Down Expand Up @@ -33,8 +33,8 @@
#' Unlike other continuous scales, secondary axis transformations for date and datetime scales
#' must respect their primary POSIX data structure.
#' This means they may only be transformed via addition or subtraction, e.g.
#' `~. + hms::hms(days = 8)`, or
#' `~.- 8*60*60`. Nonlinear transformations will return an error.
#' `~ . + hms::hms(days = 8)`, or
#' `~ . - 8*60*60`. Nonlinear transformations will return an error.
#' To produce a time-since-event secondary axis in this context, users
#' may consider adapting secondary axis labels.
#'
Expand All @@ -43,16 +43,16 @@
#' geom_point()
#'
#' # Create a simple secondary axis
#' p + scale_y_continuous(sec.axis = sec_axis(~.+10))
#' p + scale_y_continuous(sec.axis = sec_axis(~ . + 10))
#'
#' # Inherit the name from the primary axis
#' p + scale_y_continuous("Miles/gallon", sec.axis = sec_axis(~.+10, name = derive()))
#' p + scale_y_continuous("Miles/gallon", sec.axis = sec_axis(~ . + 10, name = derive()))
#'
#' # Duplicate the primary axis
#' p + scale_y_continuous(sec.axis = dup_axis())
#'
#' # You can pass in a formula as a shorthand
#' p + scale_y_continuous(sec.axis = ~.^2)
#' p + scale_y_continuous(sec.axis = ~ .^2)
#'
#' # Secondary axes work for date and datetime scales too:
#' df <- data.frame(
Expand All @@ -75,12 +75,15 @@
#' # or to transform axes for different timezones
#' ggplot(df, aes(x = dx, y = price)) + geom_line() +
#' scale_x_datetime("GMT", date_labels = "%b %d %I %p",
#' sec.axis = sec_axis(~. + 8*3600, name = "GMT+8",
#' sec.axis = sec_axis(~ . + 8 * 3600, name = "GMT+8",
#' labels = scales::time_format("%b %d %I %p")))
#'
#' @export
sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver()) {
if (!is.formula(trans)) stop("transformation for secondary axes must be a formula", call. = FALSE)
# sec_axis() historically accpeted two-sided formula, so be permissive.
if (length(trans) > 2) trans <- trans[c(1,3)]

trans <- rlang::as_function(trans)
ggproto(NULL, AxisSecondary,
trans = trans,
name = name,
Expand Down Expand Up @@ -142,20 +145,15 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
# Inherit settings from the primary axis/scale
init = function(self, scale) {
if (self$empty()) return()
if (!is.formula(self$trans)) stop("transformation for secondary axes must be a formula", call. = FALSE)
if (!is.function(self$trans)) stop("transformation for secondary axes must be a function", call. = FALSE)
if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name
if (is.derived(self$breaks)) self$breaks <- scale$breaks
if (is.waive(self$breaks)) self$breaks <- scale$trans$breaks
if (is.derived(self$labels)) self$labels <- scale$labels
},

transform_range = function(self, range) {
range <- new_data_frame(list(. = range))
rlang::eval_tidy(
rlang::f_rhs(self$trans),
data = range,
env = rlang::f_env(self$trans)
)
self$trans(range)
},

mono_test = function(self, scale){
Expand Down
14 changes: 7 additions & 7 deletions man/sec_axis.Rd

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