Skip to content
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

Feature request: rescale transformation #415

Closed
teunbrand opened this issue Dec 15, 2023 · 5 comments
Closed

Feature request: rescale transformation #415

teunbrand opened this issue Dec 15, 2023 · 5 comments
Labels
feature a feature request or enhancement

Comments

@teunbrand
Copy link
Contributor

I'd like to propose a new transformation function that essentially wraps the rescale() function. It's definition can be something like the following:

library(scales)

transform_rescale <- function(to = c(0, 1), from = c(0, 1)) {
  force(to)
  force(from)
  new_transform(
    "rescaling",
    transform = function(x) rescale(x, to = to, from = from),
    inverse   = function(x) rescale(x, to = from, from = to) 
  )
}

It should be able to handle a bunch of 'scale and translate' transformations, like unit conversions. Going from metres to inches is simply a scale transformation.

metres_to_inches <- transform_rescale(to = c(0, 100/2.54))
metres_to_inches$transform(1)
#> [1] 39.37008

Whereas going from degrees celcius to degrees fahrenheit is a 'scale and translate' transformation.

celsius_to_fahrenheit <- transform_rescale(to = c(32, 212), from = c(0, 100))
celsius_to_fahrenheit$transform(-5)
#> [1] 23
celsius_to_fahrenheit$transform(32)
#> [1] 89.6

Created on 2023-12-15 with reprex v2.0.2

Given that it is suitable for plenty of unit conversions, I think this transformation may have a bunch of applications.

@mjskay
Copy link
Contributor

mjskay commented Dec 16, 2023

In case it is desired, adding derivative support for this should entail something like:

transform_rescale <- function(to = c(0, 1), from = c(0, 1)) {
  force(to)
  force(from)
  new_transform(
    "rescaling",
    transform = function(x) rescale(x, to = to, from = from),
    inverse   = function(x) rescale(x, to = from, from = to),
    d_transform = function(x) rep(diff(to) / diff(from), length(x)),
    d_inverse = function(x) rep(diff(from) / diff(to), length(x))
  )
}

@teunbrand
Copy link
Contributor Author

Ah yes, of course! Thanks for weighing in, it was a little oversight on my part

@thomasp85
Copy link
Member

Happy to accept a PR for this :-)

@thomasp85 thomasp85 added the feature a feature request or enhancement label Oct 22, 2024
@teunbrand
Copy link
Contributor Author

There was something fundamentally flawed with this approach when I last tried it. Unfortunately, I have forgotten what it was, so let me try again :)

@teunbrand
Copy link
Contributor Author

teunbrand commented Oct 22, 2024

I've rediscovered why this approach is flawed. Essentially, you calculate breaks and labels in 'original' space, so the transformation is essentially a fancyful transform_identity() in disguise.

library(scales)

transform_rescale <- function(to = c(0, 1), from = c(0, 1)) {
  scales:::force_all(to, from)
  new_transform(
    "rescaling",
    transform   = function(x) rescale(x, to = to, from = from),
    inverse     = function(x) rescale(x, to = from, from = to),
    d_transform = function(x) rep(diff(to) / diff(from), length(x)),
    d_inverse   = function(x) rep(diff(from) / diff(to), length(x))
  )
}

celcius_to_fahrenheit <- transform_rescale(to = c(32, 212), from = c(0, 100))

library(ggplot2)

# Note the original range in Celcius:
range(pressure$temperature)
#> [1]   0 360

ggplot(pressure, aes(temperature, pressure)) +
  geom_line() +
  scale_x_continuous(
    transform = celcius_to_fahrenheit
  )

Created on 2024-10-22 with reprex v2.1.1

It might be easy enough to calculate the default breaks in the transformed space, but that won't mesh well if people give custom breaks in ggplot2.

I'll just go ahead and close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants