-
Notifications
You must be signed in to change notification settings - Fork 109
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
Comments
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))
)
} |
Ah yes, of course! Thanks for weighing in, it was a little oversight on my part |
Happy to accept a PR for this :-) |
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 :) |
I've rediscovered why this approach is flawed. Essentially, you calculate breaks and labels in 'original' space, so the transformation is essentially a fancyful 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. |
I'd like to propose a new transformation function that essentially wraps the
rescale()
function. It's definition can be something like the following: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.
Whereas going from degrees celcius to degrees fahrenheit is a 'scale and translate' transformation.
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.
The text was updated successfully, but these errors were encountered: