Add sym_limits function#133
Conversation
damonbayer
left a comment
There was a problem hiding this comment.
Thanks @dylanhmorris!
It would be great if this could be implemented to more directly work with ggplot2, something like scale_y_symmetric. The LLM results I got based on this idea were pretty ugly looking, though.
|
@dylanhmorris what do you think about using |
sym_lim <- function(values, center = 0, transform = "identity") {
transform_fn <- scales::as.transform(transform)
transformed_values <- transform_fn$transform(values)
transformed_center <- transform_fn$transform(center)
span <- max(abs(transformed_values - transformed_center))
transform_fn$inverse(transformed_center + c(-span, span))
}
sym_lim(values = c(3, 5))
#> [1] -5 5
sym_lim(values = c(0.4, 10), center = 1, transform = "log10")
#> [1] 0.1 10.0
sym_lim(values = c(2, 9), center = 4, transform = "sqrt")
#> [1] 1 9
sym_lim(values = c(1, 100), center = 1, transform = "log1p")
#> [1] -0.960396 100.000000Created on 2025-05-15 with reprex v2.1.1 |
I love this! PR it into this branch @damonbayer? |
sym_limits and sym_limits_log functionssym_limits function
|
@damonbayer What do you think about making the center value default to That way you'd get sensible defaults and avoid problems when 0-on-the-untransformed scale is outside (or at the edge of) the domain of the transform, e.g. sym_limits(c(5, 2), transform = "log10") |
I dig it. Great heuristic for getting the "natural" center of a scale. Let's swap the order of the |
|
LMK how this looks @damonbayer. Feel free to merge if it looks good. |
Closes #30