Skip to content

Commit

Permalink
add support for lims() in ggplot2 scales, closes #370
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Jul 15, 2024
1 parent 689fa1a commit 123c23d
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 10 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: units
Version: 0.8-5.5
Version: 0.8-5.6
Title: Measurement Units for R Vectors
Authors@R: c(person("Edzer", "Pebesma", role = c("aut", "cre"), email = "edzer.pebesma@uni-muenster.de", comment = c(ORCID = "0000-0001-8049-7069")),
person("Thomas", "Mailund", role = "aut", email = "mailund@birc.au.dk"),
Expand Down Expand Up @@ -41,7 +41,7 @@ SystemRequirements: udunits-2
License: GPL-2
URL: https://r-quantities.github.io/units/, https://github.com/r-quantities/units
BugReports: https://github.com/r-quantities/units/issues
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Roxygen: list(old_usage = TRUE)
Encoding: UTF-8
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

* Fix `hist()` error; #368

* Add support for `lims()` in `ggplot2` scales; #370

# version 0.8-5

* avoid -Wformat-security warning on CRAN
Expand Down
34 changes: 26 additions & 8 deletions R/scale_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ scale_x_units <- function(..., guide = ggplot2::waiver(), position = "bottom",
palette = identity, ...,
guide = guide,
position = position,
super = MakeScaleContinuousPositionUnits()
super = make_scale_units()
)
sc$units <- as_units(unit)
set_sec_axis(sec.axis, sc)
Expand All @@ -76,16 +76,16 @@ scale_y_units <- function(..., guide = ggplot2::waiver(), position = "left",
palette = identity, ...,
guide = guide,
position = position,
super = MakeScaleContinuousPositionUnits()
super = make_scale_units()
)
sc$units <- as_units(unit)
set_sec_axis(sec.axis, sc)
}

MakeScaleContinuousPositionUnits <- function() {
make_scale_units <- function(parent=ggplot2::ScaleContinuousPosition) {
ggplot2::ggproto(
"ScaleContinuousPositionUnits",
ggplot2::ScaleContinuousPosition,
parent,
units = NULL,

map = function(self, x, limits = self$get_limits()) {
Expand All @@ -95,16 +95,19 @@ MakeScaleContinuousPositionUnits <- function() {
else units(x) <- as_units(1, self$units)
x <- drop_units(x)
}
ggplot2::ggproto_parent(
ggplot2::ScaleContinuousPosition, self)$map(x, limits)
ggplot2::ggproto_parent(parent, self)$map(x, limits)
},

transform = function(self, x) {
if (!is.null(self$units))
units(x) <- as_units(1, self$units)

new_x <- ggplot2::ggproto_parent(
ggplot2::ScaleContinuousPosition, self)$transform(drop_units(x))
if (inherits(self$limits, "units")) {
units(self$limits) <- units(x)
self$limits <- drop_units(self$limits)
}

new_x <- ggplot2::ggproto_parent(parent, self)$transform(drop_units(x))
as_units(new_x, units(x))
},

Expand Down Expand Up @@ -135,3 +138,18 @@ scale_type.units <- function(x) {
" Please, attach it using 'library(units)' to properly show scales with units.")
c("units", "continuous")
}

utils::globalVariables("caller_env")

# registered in .onLoad()
limits.units <- function(lims, var, call = caller_env()) {
if (length(lims) != 2)
stop("`", var, "` must be a two-element vector", call.=FALSE)

trans <- if (!any(is.na(lims)) && lims[1] > lims[2])
"reverse" else "identity"
name <- paste0("scale_", var, "_units")
sc <- match.fun(name)(limits=lims, transform=trans)
sc$call <- if (!is.null(call)) call else str2lang(paste0(name, "()"))
sc
}
1 change: 1 addition & 0 deletions R/tidyverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ register_all_s3_methods <- function() {
register_s3_method("vctrs::vec_ptype2", "units.units")
register_s3_method("vctrs::vec_cast", "units.units")
register_s3_method("ggplot2::scale_type", "units")
register_s3_method("ggplot2::limits", "units")
}

register_s3_method <- function(generic, class, fun=NULL) {
Expand Down
65 changes: 65 additions & 0 deletions tests/testthat/_snaps/plot/ggplot2-limits-other-units.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions tests/testthat/_snaps/plot/ggplot2-limits-via-scale-with-units.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions tests/testthat/_snaps/plot/ggplot2-limits-via-scale.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 123c23d

Please sign in to comment.