Skip to content

Add sym_limits function#133

Merged
damonbayer merged 12 commits into
mainfrom
dhm-add-sym-limits
May 15, 2025
Merged

Add sym_limits function#133
damonbayer merged 12 commits into
mainfrom
dhm-add-sym-limits

Conversation

@dylanhmorris

Copy link
Copy Markdown
Collaborator

Closes #30

@dylanhmorris
dylanhmorris requested a review from damonbayer as a code owner May 15, 2025 00:56
Comment thread R/utils.R Outdated
@dylanhmorris
dylanhmorris requested a review from damonbayer May 15, 2025 13:44

@damonbayer damonbayer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@damonbayer

Copy link
Copy Markdown
Collaborator

@dylanhmorris what do you think about using scales to support a wider range of transformations in this function?

@damonbayer

damonbayer commented May 15, 2025

Copy link
Copy Markdown
Collaborator

@dylanhmorris

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.000000

Created on 2025-05-15 with reprex v2.1.1

@dylanhmorris

dylanhmorris commented May 15, 2025

Copy link
Copy Markdown
Collaborator Author

@dylanhmorris what do you think about using scales to support a wider range of transformations in this function?

I love this! PR it into this branch @damonbayer?

@dylanhmorris dylanhmorris changed the title Add sym_limits and sym_limits_log functions Add sym_limits function May 15, 2025
@dylanhmorris

dylanhmorris commented May 15, 2025

Copy link
Copy Markdown
Collaborator Author

@damonbayer What do you think about making the center value default to NULL and using "0 on the transformed scale" as the center value if the user doesn't set it, but having the user specify center values on the untransformed scale?

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")

@damonbayer

damonbayer commented May 15, 2025

Copy link
Copy Markdown
Collaborator

@damonbayer What do you think about making the center value default to NULL and using "0 on the transformed scale" as the center value if the user doesn't set it, but having the user specify center values on the untransformed scale?

I dig it. Great heuristic for getting the "natural" center of a scale. Let's swap the order of the center and transform arguments, then.

@dylanhmorris
dylanhmorris requested a review from damonbayer May 15, 2025 16:12
@dylanhmorris

dylanhmorris commented May 15, 2025

Copy link
Copy Markdown
Collaborator Author

LMK how this looks @damonbayer. Feel free to merge if it looks good.

Comment thread tests/testthat/test_utils.R Outdated
Comment thread tests/testthat/test_utils.R Outdated
Comment thread tests/testthat/test_utils.R Outdated
@damonbayer
damonbayer enabled auto-merge (squash) May 15, 2025 16:17
@damonbayer
damonbayer merged commit d41055e into main May 15, 2025
@damonbayer
damonbayer deleted the dhm-add-sym-limits branch May 15, 2025 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create symmetric axis limits helper function

2 participants